﻿
************************************************************************************************
**       NOTE THIS PROGRAM IS REORGANIZED LIST BASED ON EBERT 2.0 08212015.SAS.               **
**              It is specifically designed for analysis of data from Asian Citrus Psyllid.   **
**                 However, it will work on any insect with behaviors NP, C, G, D, E1, and E2.**
**                 Use of behavior Z as well as NP will require careful modifications to the  **
**                 program. If behaviors A and B are added, please consider "Ebert Generic"   **
**                 for analysis of those behaviors. Note that including A and B behaviors will**
**                 modify the meaning of C in this program.                                   **
**              This program  is based on the SAS mimic of the Sarria workbook (Ebert 1.0)    **
**              but the order of the variables has been changed. Furthermore,                 **
**              the variables not relevant to psyllids have been removed.                     **
**              The SAS sataset Ebert retains the names of these variables in the code, so it **
**              would be possible to return them to the output                                **
**              with a Proc glimmix statement (should they become needed).                    **
**            Finally, some new variables have been added. Specifically variables associated  **
**              with G and D. Also variables dealing with standard deviation of the mean      **
**              durations for Np, C, D, E1, E2, and G. Finally the mean duration for probes   **
**              as well as the standard deviation and median duration for probes. All         **
**              these new variables are by insect.                                            **
**                                                                                            **
**             In this version of the program there are 89 active variables.                  **
************************************************************************************************
************************************************************************************************;

*********************   CRITICAL INFORMATION   **********************
*********************************************************************
**    Input data must be in this order:                            **
**		Insect Number                                              **
**		Waveform                                                   **
**		Duration of behavior in seconds                            **
**    Adjust the read satatemnt if other variables are present.    **
**    Observation times must be in seconds                         **
*********************************************************************
**************************   Data sets List      ********************
**	one: the raw data set with Insectno, Waveform, and Durations
**	two: Ebert variables
**	three: a dataset used to extract information from dataset one
**	OnlyE1: a dataset with insects that all express E1 at least once
**	OnlyE2: a dataset with insects that all express E2 at least once
**	OnlySusE2: only insects having E2 longer than 10 minutes
**	OnlyNP: only NP waveform
**	OnlyC: only C waveform
**	OnlyG: only G waveform
**	OnlyE1: only E1 waveform
**	OnlyE2: only E2 waveform
**	Ebert: All the variables in Sarria relevant to Psyllids (Asian Citrus Psyllid), plus some additional variables. 
**	
*********************************************************************
*********************************************************************
********************    Variables List     **************************
**	Insectno: The unique identifier for each individual in dataset
**	Waveform: The unique 3 character identifier for behavior
*****		Current options: Z C NP F G E1 E2 D PD II1 II2 E1e

**	Dur: The duration in seconds for each behavior
**	Book keeping variables: ino, w0, w1, marker1, marker2, holder1
**	Line: Variable used to restore chronological order
**	Inverter1: Variable used to reverse chronological order
**	Instance: The number of times a given behavior is recorded
**	Sumstart: time from start of recording to start of behavior
**	SumEnd: time from start of recording to end of behavior

*********************************************************************
*********************************************************************;

*********************************************************************
*****************   Log of Changes   ********************************
***  12-15-2016  Variable sdg was calculated with dataset         ***
***                 OnlyD. It is now calculated with OnlyG        ***
***              Code associated with waveforms                   ***
***                 pd, e1e, and F were deleted.                  ***
*********************************************************************;
*Treatments are as follows   ***************
A = Female Brown
B = Female Green
C = Male Brown
D = Male Green
*****************************;

options ls=100 ps=72;
* Input statement for basic testing and program development. It is replaced when program is used for Data analyses.;
Data one(keep=insectno waveform dur);
	infile "C:\Users\tebert\OneDrive - University of Florida\Work\Manuscipts\Gender size\Weight 2\Weight.csv" dsd missover firstobs=2 delimiter=',' end=last;
	length  insectno$ 20 waveform$ 10 dur 8; *specifies record lengths for reading variables;
	input  insectno$ waveform$ dur; *creates variable names for input. The $ character tells SAS to treat these variables as charaters not numbers;

    waveform=compress(upcase(waveform));
    if waveform='Z' then waveform='NP';
run;

data one; set one;
    *insectno=compress(trt||insectno);
    proc sort; by insectno;

*ODS noresults; *suppresses output to "results" and "output" windows.;
* Output statement for basic testing and program development. It is replaced when program is used for Data analyses.;
*ODS HTML file='C:\Users\tebert\Desktop\Control Data3 041715N1b'; *Directs all output to this file.;

Data one; set one;
	Transform=1;	** If transform=1 then all the data will be transformed as per "Transformations" section.   ***;
					** If transform=0 then NONE of the data will be transformed.                                ***;
      line=_n_;
*     Calculate time to start and time to end of each behavior.;
      retain in0 SumStart SumEnd dur0;
      if insectno ne in0 then do;
       SumStart=0.0; SumEnd=0.0; dur0=0.0;
       in0=insectno;
      end;
      SumEnd=SumEnd+dur;
      SumStart=SumStart+Dur0;
      dur0=dur;
	proc sort;by insectno waveform line;
data one; set one;
if dur ne "." then output;

data one; set one; proc sort; by insectno waveform;
data one;set one; by insectno waveform;
	retain instance;
	if first.waveform then instance=0;
	instance=instance+1;
Data one; set one; proc sort; by line;
Data one; set one;
	inverter1=50000-line;
Data one; set one; drop in0 dur0;

Data one; set one;
retain in0 holder1;
if in0 ne insectno then do; in0=insectno; holder1=0; end;
holder1 = holder1+dur;
data one; set one; drop in0;
data one; set one; proc sort; by inverter1;
data one; set one;
retain in0 maxdur;
if in0 ne insectno then do; in0=insectno; maxdur=holder1; end;
data one; set one; drop in0 holder1;
data one; set one; proc sort; by line;
Run;

*********************************************************************
*********************************************************************
******        This section calculates data sets guaranteed       ****
******         that every insect in that dataset will have       ****
******         the relevant waveform at least once.              ****
*********************************************************************
*********************************************************************

*********************************************************************
*******                             Start New Method              ***
*******                        define the dataset OnlyG           ***
*********************************************************************;
Data three; set one; Proc sort; by insectno line;
Data three; set three;
	retain in0 marker1;
	if insectno ne in0 then do;
		in0=insectno; marker1=0; 
	end;
    if compress(upcase(waveform))='G' then Marker1=1;
Data three; set three;
Proc sort; by insectno Inverter1;
Data three; set three; drop in0;
Data three; Set three;
	retain in0 marker2;
	if insectno ne in0 then do;
	  marker2=0; in0=insectno; 
	end;
	if marker1=1 then marker2=1;
	if marker1=0 and marker2=1 then marker2=1;
Data three; set three;
if marker2=1 then output;
Data three; set three; Proc sort; by insectno line;
data three; set three; drop marker1 marker2 in0;
Data OnlyG; Set three;
run;
*********************************************************************
*  Finished creating dataset OnlyG
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*;


*********************************************************************
*******                            Start New Method               ***
*******                        define the dataset OnlyD           ***
*********************************************************************;
Data three; set one; Proc sort; by insectno line;
Data three; set three;
	retain in0 marker1;
	if insectno ne in0 then do;
		in0=insectno; marker1=0; 
	end;
    if compress(upcase(waveform))='D' then Marker1=1;
Data three; set three;
Proc sort; by insectno Inverter1;
Data three; set three; drop in0;
Data three; Set three;
	retain in0 marker2;
	if insectno ne in0 then do;
	  marker2=0; in0=insectno; 
	end;
	if marker1=1 then marker2=1;
	if marker1=0 and marker2=1 then marker2=1;
Data three; set three;
if marker2=1 then output;
Data three; set three; Proc sort; by insectno line;
data three; set three; drop marker1 marker2 in0;
Data OnlyD; Set three;
run;
*********************************************************************
*  Finished creating dataset OnlyD
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*;

*********************************************************************
*******                             Start New Method              ***
*******                        define the dataset OnlyE1          ***
*********************************************************************;
Data three; set one; Proc sort; by insectno line;
Data three; set three;
	retain in0 marker1;
	if insectno ne in0 then do;
		in0=insectno; marker1=0; 
	end;
    if compress(upcase(waveform))='E1' then Marker1=1;
Data three; set three;
Proc sort; by insectno Inverter1;
Data three; set three; drop in0;
Data three; Set three;
	retain in0 marker2;
	if insectno ne in0 then do;
	  marker2=0; in0=insectno; 
	end;
	if marker1=1 then marker2=1;
	if marker1=0 and marker2=1 then marker2=1;
Data three; set three;
if marker2=1 then output;
Data three; set three; Proc sort; by insectno line;
data three; set three; drop marker1 marker2 in0;
Data OnlyE1; Set three;
run;
*********************************************************************
*  Finished creating dataset OnlyE1
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*;

*********************************************************************
*******                             Start New Method              ***
*******                        define the dataset OnlyE2          ***
*********************************************************************;
Data three; set one; Proc sort; by insectno line;
Data three; set three;
	retain in0 marker1;
	if insectno ne in0 then do;
		in0=insectno; marker1=0; 
	end;
    if compress(upcase(waveform))='E2' then Marker1=1;
Data three; set three;
Proc sort; by insectno Inverter1;
Data three; set three; drop in0;
Data three; Set three;
	retain in0 marker2;
	if insectno ne in0 then do;
	  marker2=0; in0=insectno; 
	end;
	if marker1=1 then marker2=1;
	if marker1=0 and marker2=1 then marker2=1;
Data three; set three;
if marker2=1 then output;
Data three; set three; Proc sort; by insectno line;
data three; set three; drop marker1 marker2 in0;
Data OnlyE2; Set three;
run;
*********************************************************************
*  Finished creating dataset OnlyE2
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*;


*********************************************************************
*******                             Start New Method              ***
*******                        define the dataset OnlySusE2       ***
*********************************************************************;
Data three; set one; Proc sort; by insectno line;
Data three; set three;
	retain in0 marker1;
	if insectno ne in0 then do;
		in0=insectno; marker1=0; 
	end;
    if compress(upcase(waveform))='E2' and dur>600 then Marker1=1;
Data three; set three;
Proc sort; by insectno Inverter1;
Data three; set three; drop in0;
Data three; Set three;
	retain in0 marker2;
	if insectno ne in0 then do;
	  marker2=0; in0=insectno; 
	end;
	if marker1=1 then marker2=1;
	if marker1=0 and marker2=1 then marker2=1;
Data three; set three;
if marker2=1 then output;
Data three; set three; Proc sort; by insectno line;
data three; set three; drop marker1 marker2 in0;
Data OnlySusE2; Set three;
run;
*********************************************************************
*  Finished creating dataset OnlySusE2
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*;


proc datasets nodetails nolist; delete three;
*********************************************************************;
*********************************************************************
****   New Data set, OnlyPrbs. This converts all recordings into  ***
****      probe versus non-probe.                                 ***
*********************************************************************;
Data OnlyPrbs; set one;
	if compress(upcase(waveform))='NP' then waveform='NP';
		else waveform='C';
Data OnlyPrbs; Set OnlyPrbs; proc sort; by line;
Data OnlyPrbs; Set OnlyPrbs;
	retain w0 w1 in0 marker1;
	w1=Compress(upcase(waveform));
	if insectno ne in0 then do;
	  w0='  '; in0=insectno; marker1=0;
	end;
	if w1 ne w0 then do; 
		marker1=marker1+1;
		w0=w1;
	end;
data OnlyPrbs; set OnlyPrbs;
proc means noprint; by insectno marker1; var dur; output out=onePrbSAS sum=durSum;
Data onePrbSAS; set onePrbSAS; dur=dursum;

data OnlyPrbs; set OnlyPrbs; drop dur line sumstart sumend instance inverter1 w1 w0 in0;
Data OnlyPrbs; Set OnlyPrbs;
	retain w0 w1 in0 time1;
	w1=Compress(upcase(waveform));
	if insectno ne in0 then do;
	  w0='  '; in0=insectno; time1=0;
	end;
	if time1=0 then do; output; time1=1; end;
	else If w1 ne w0 then output;
	w0=w1;
data onePrbSAS; set onePrbSAS OnlyPrbs; merge onePrbSAS OnlyPrbs; by insectno marker1;
data oneD; set onePrbSAS;  Var1=insectno; Var2=waveform; Var3=dur;
data oneD; set oneD; drop insectno marker1 _TYPE_ _Freq_ dursum dur waveform;
Data oneD; set oneD; waveform=Var2; insectno=Var1; dur=Var3;
data oneD; set oneD; drop Var1 var2 var3;
Data OnlyPrbs; set oneD; line=_n_;
Data OnlyPrbs; Set OnlyPrbs; drop in0 w1 w0;
Data OnlyPrbs; Set OnlyPrbs;
      retain in0 SumStart SumEnd dur0;
      if insectno ne in0 then do;
       SumStart=0.0; SumEnd=0.0; dur0=0.0;
       in0=insectno;
      end;
      SumEnd=SumEnd+dur;
      SumStart=SumStart+Dur0;
      dur0=dur;
proc sort; by insectno waveform line;
data OnlyPrbs; set OnlyPrbs; drop in0 dur0;
data OnlyPrbs;set OnlyPrbs; by insectno waveform;
	retain instance;
	if first.waveform then instance=0;
	instance=instance+1;
data OnlyPrbs; set OnlyPrbs; proc sort; by line;
data OnlyPrbs; set OnlyPrbs; inverter1=50000-line;
data OnlyPrbs; set OnlyPrbs; drop time1;
proc datasets nodetails nolist; delete oneD onePrbSAS;
run;
*********************************************************************
**************************   Method end   ***************************
*********************************************************************;


*********************************************************************
****               THE END. START Computing Variables      **********
*********************************************************************
*********************************************************************
****             ******    ***********   *****       ****************
****             ******     **********   *****   ***    *************
****   ****************      *********   *****   ******   ***********
****   ****************   *   ********   *****   ********   *********
****   ****************   **   *******   *****   *********   ********
****             ******   ***   ******   *****   **********   *******
****             ******   ****   *****   *****   *********   ********
****   ****************   *****   ****   *****   ********   *********
****   ****************   ******   ***   *****   *******   **********
****   ****************   *******   **   *****   *****   ************
****             ******   ********   *   *****   ***   **************
****             ******   *********      *****       ****************
*********************************************************************
*********************************************************************;

*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************
*******                                                      ********
*******                           Variables                  ********
*******                                                      ********
*******               Initial Varibales are similar to       ********
*******                 those in the Sarria workbook         ********
*******                                                      ********
*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************
*********************************************************************;


*****************************   Start New Method    *****************
Time from beginning of EXPT to first probe (waveform C)
*********************************************************************;

Data Ebert; Set one;
retain in0;
if insectno ne in0 then do; in0=insectno; if waveform="NP" then TmFrstPrbFrmStrt=sumend; else TmFrstPrbFrmStrt="."; end;
Data Ebert; Set Ebert;
 drop in0;
 Data Ebert; Set Ebert;
 retain in0 marker1;
 if in0 ne insectno then do; in0=insectno; marker1=0; end; else marker1=1;
 Data Ebert; Set Ebert; if marker1=0 then output;

data Ebert; set Ebert; drop sumstart sumend line instance waveform dur inverter1 in0 marker1;
run;

*********************************************************************
*  Finished time from start of experiment to first probe.      ******
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*;


*********************************************************************
/*********************************   Start New Method         *******
*  Number of probes to first E1.                              *******
*  Eliminate all insects without an E1 (data set OnlyE1).     *******
*  Drop all extraneous waveforms.                             *******
*  Set marker1 to 1, and it changes to 0 at the first E1.     *******
*  Delete all marker1=0.                                      *******
*********************************************************************;

Data three; set OnlyE1;
Data three; set three; Proc sort; by insectno line;
Data three; set three;
	retain in0 marker1 marker2;
	w1=Compress(upcase(waveform));
	if insectno ne in0 then do;
	 marker1=0; Marker2=0;
	 in0=insectno;
	end;
	If w1='C' then marker1=1;
	If w1='Z' or w1='NP' then marker1=0;
	if w1='E1' then marker2=1;
Data three; set three; if marker2=0 then output;
data three; set three; drop marker2 in0;
Data three; set three;
	retain in0 marker3 marker4;
	if insectno ne in0 then do;
	marker3=0; marker4=0; in0=insectno;
	end;
	if marker1=1 and marker3=0 then marker4=marker4+1;
	marker3=marker1;
Data three; set three; drop in0 marker1 marker3;
Data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain marker1 in0;
	if insectno ne in0 then do;
	marker1=0; in0=insectno;
	end;
	else marker1=1;
Data three; set three; if marker1=0 then output;
Data three; set three; CtoFrstE1=marker4;
Data three; Set three; drop waveform dur line sumstart sumend instance inverter1 w1 in0 marker4 marker1;
data Ebert; set Ebert three; merge Ebert three; by insectno;
run;
*********************************************************************
*  Finding number of probes to first E1 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*********************************************************************
*********************************   Start New Method              ***
*  Duration of First and Second Probe.                            ***
*  Combine all non-NP and non-Z waveforms into a new waveform "P".***
*  Sum the durations for all consecutive P.                       ***
*  Find last element in any sum, and delete everything else.      ***
*  Rework instance based on new waveform list.                    ***
*  eliminate all but instance=1, then all but instance=2.         ***
*  Delete extraneous variables and merge.                         ***
*********************************************************************;
Proc datasets nolist nodetails; delete three outsas;
Data three; set one;
	if compress(upcase(waveform))='NP' or compress(upcase(waveform))='Z' then do marker1=1; waveform="Z"; end; else do marker1=0; waveform="P"; end;
Data three; set three;
	retain W1 W0 in0 marker2;
	if in0 ne insectno then do;
		in0=insectno; marker2=0; W1=0;
	end;
	W0=marker1;
	if W1 ne W0 then marker2=marker2+1;
	W1=W0;
Data three; set three;	drop in0 w1 w0;
data three; set three;
	retain W1 W0 in0 Sdur;
	if in0 ne insectno then do;
		in0=insectno; Sdur=0; W1=0;
	end;
	W0=marker2;
	if W0 eq w1 then sdur=sdur+dur; else sdur=dur;
	w1=w0;
data three; set three; drop in0 w1 w0;
data three; set three;
retain in0;
if in0 ne insectno then do;
	in0=insectno;
*	if waveform eq "P" then sdur="."; *Activate this line to delete first probe if recording not start in NP;
	end;
Data three; set three; drop in0; 
data three; set three; proc sort; by inverter1;
data three; set three;
	retain in0 w1 w0 marker4;
	if in0 ne insectno then do;
		in0=insectno; marker4=0; w1='   ';
	end;
	w0=waveform;
	if w1 ne w0 then marker4=0; else marker4=1;
	w1=w0;
data three; set three; if marker4=0 then output;
data three; set three; proc sort; by line;
data three; set three; drop instance w1 w0 in0;
data three; set three; proc sort; by insectno waveform;
data three;set three; by insectno waveform;
	retain instance;
	if first.waveform then instance=0;
	instance=instance+1;
data three; set three; if waveform="P" then output;
data four; set three; if instance=1 then output;
data five; set three; if instance=2 then output;
data four; set four; DurFrstPrb=sdur; drop sdur waveform dur line sumstart sumend inverter1 marker1 marker2 marker4 instance;
data five; set five; DurScndPrb=sdur; drop sdur waveform dur line sumstart sumend inverter1 marker1 marker2 marker4 instance;
data Ebert; set Ebert four; merge Ebert four; by insectno;
data Ebert; set Ebert five; merge Ebert five; by insectno;
proc datasets nolist nodetails; delete four five three;

*********************************************************************
*  Finding duration of First and Second probe is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*********************************************************************
*********************************   Start New Method         ********
*	Duration of shortest C before first E1 in any probe.     ********
*  "Duration of shortest C before E1"                        ********
*********************************************************************;
Data three; set one; Proc sort; by insectno line;
data three; set three;
	if compress(upcase(waveform))='NP' or compress(upcase(waveform))='Z' then waveform='Z'; else if compress(upcase(waveform))='E1' then waveform='E1'; else waveform='P';
data three; set three;
	retain in0 marker1 w0;
	if in0 ne insectno then do;
	  in0=insectno; marker1=0; w0='  ';
	end;
	if w0='E1' then marker1=1;
	if w0='Z' then marker1=0;
	w0=waveform;
data three; set three; if waveform='Z' then marker1=0;
data three; set three; if marker1=0 then output;
data three; set three; proc sort; by inverter1;
data three; set three; drop in0 w0 marker1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
	  in0=insectno; marker1=0;
	end;
	if waveform='E1' then marker1=1;
	if waveform='Z' then marker1=0;
Data three; set three; proc sort; by line;
data three; set three; if marker1=1 then output;


Data three; set three;	drop in0 marker1;
data three; set three;
	retain W1 W0 in0 Sdur;
	if in0 ne insectno then do;
		in0=insectno; Sdur=dur; W1='  ';
	end;
	W0=compress(upcase(waveform));
	if W0 eq w1 then sdur=sdur+dur; else sdur=dur;
	w1=w0;

data three; set three; proc sort; by inverter1;
data three; set three; drop in0 w1 w0;
data three; set three;
	retain in0 w1 w0 marker4;
	if in0 ne insectno then do;
		in0=insectno; marker4=0; w1=' ';
	end;
	w0=waveform;
	if w1 ne w0 then marker4=0; else marker4=1;
	w1=w0;
data three; set three; if marker4=0 then output;
data three; set three; proc sort; by line;
data three; set three; drop instance w1 w0 in0;
data three; set three; proc sort; by insectno waveform;
data three;set three; by insectno waveform;
	retain instance;
	if first.waveform then instance=0;
	instance=instance+1;
data three; set three; if waveform="P" then output;
data three; set three; proc sort; by insectno;
data three; set three; proc means noprint; by insectno; var Sdur; output out=outsas min=ShrtCbfrE1;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
Data Ebert; Set Ebert; drop _TYPE_ _FREQ_;
proc datasets nolist nodetails; delete three outsas; 

*********************************************************************
*  Finding duration of shortest C event before an E1.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*********************************************************************
*********************************   Start New Method       **********
*  Duration of second non-probe event (z or np).           **********
*  Eliminate all waveforms except z and np.                **********
*  Count line numbers and extract the second behavior.     **********
*  Delete extraneous variables and merge.                  **********
*********************************************************************;
data three; set one;
retain in0 holder4;
if in0 ne insectno then do; in0=insectno; holder4=waveform; end;
data three; set three; drop in0;
data three; set three;
	if  compress(upcase(waveform))='Z' or compress(upcase(waveform))='NP' then output;
data three; set three;
	if holder4="NP" or holder4="Z" then instance=instance; else instance=instance+1;
data three; set three; if instance=2 then output;
Data three; set three;
	DurScndZ=dur;
	drop waveform line dur instance sumstart sumend inverter1 holder4;
Data Ebert; set Ebert three;
	merge Ebert three;
	by insectno;
proc datasets nolist nodetails; delete three;
*********************************************************************
*  Finding duration of Second non-probe event is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*********************************************************************
**                              Start New Method         ************
**	Duration of nonprobe period before the first e1      ************
*********************************************************************;
Data three; set One; *Using dataset OnlyE1 will change insects without E1 to missing;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do; in0=insectno; marker1=0; end;
	if compress(upcase(waveform)) eq 'E1' then marker1=1;
Data three; set three; if marker1=0 then output;
data three; set three; drop in0 marker1;
Data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do; in0=insectno; marker1=0; end;
	if compress(upcase(waveform)) eq 'Z' or compress(upcase(waveform)) eq 'NP' then marker1=1;
Data three; set three; if marker1=1 then output;
Data three; set three; if compress(upcase(waveform)) eq 'Z' or compress(upcase(waveform)) eq 'NP' then output;
Data three; set three; drop in0 marker1;
data three; set three; proc sort; by insectno line;
data three; set three; 
	retain DurNnprbBfrFrstE1 in0;
	if in0 ne insectno then do; in0=insectno; DurNnprbBfrFrstE1=0; end;
	DurNnprbBfrFrstE1=DurNnprbBfrFrstE1+dur;
Data three; set three; drop in0;
data three; set three; proc sort; by insectno inverter1;
Data three; set three;
	retain in0 marker1;
	if insectno ne in0 then do;
		in0=insectno; marker1=1;
	end;
	else marker1=0;
Data three; set three; if marker1=1 then output;
Data three; set three; drop waveform dur line sumstart sumend instance inverter1 in0 marker1;
data Ebert; set Ebert three; merge Ebert three; by insectno;
*********************************************************************
*  Finding Duration of nonprobe period before the first e1 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*********************************************************************
*******                       Start New Method    *******************
*******       Time from start of EPG to 1st E
*********************************************************************;
Data three; set onlyE1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='E1' then  marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; proc sort; by insectno inverter1;
data three; set three; drop in0 marker1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	else marker1=1;
Data three; set three; if marker1=0 then output;
data three; set three; TmStrtEPGFrstE=sumend;
data three; set three; drop waveform dur line sumstart sumend instance inverter1 in0 marker1;
data Ebert; set Ebert three; merge Ebert three; by insectno;
proc datasets nolist nodetails; delete three;
Run;
*********************************************************************
*  Finding Time from start of EPG to 1st E is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********    END of PART 1 ******************************************
*********************************************************************;


*********************************************************************
**                                   Start New Method    ************
**	Time from first probe to 1st E                       ************
*********************************************************************;
Data three; set onlyE1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='E1' then  marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; drop in0 marker1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='C' then marker1=1;
Data three; set three; if marker1=1 then output;
Data three; set three; drop in0 marker1;
data three; set three;
	retain in0 TmFrmFrstPrbFrstE;
	if in0 ne insectno then do;
		in0=insectno; TmFrmFrstPrbFrstE=0;
	end;
	TmFrmFrstPrbFrstE = TmFrmFrstPrbFrstE + dur;
data three; set three; drop in0;
data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	else marker1=1;
data three; set three; proc sort; by line;
data three; set three; if marker1=0 then output;
data three; set three; drop waveform dur line sumstart sumend instance inverter1 in0 marker1;
data Ebert; set Ebert three; merge Ebert three; by insectno;
*********************************************************************
*  Finding Time from 1st probe to 1st E is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
**                                 Start New Method    **************
**	Time from start of probe with first E to 1st E     **************
*********************************************************************;
Data three; set onlyE1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='E1' then  marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; drop in0 marker1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='C' then marker1=1;
Data three; set three; if marker1=1 then output;
Data three; set three; drop in0 marker1;
data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='Z' or compress(upcase(waveform))='NP' then marker1=1;
data three; set three; proc sort; by line;
data three; set three; if marker1=0 then output;
Data three; set three; drop in0 marker1;
data three; set three;
	retain in0 TmBegPrbFrstE;
	if in0 ne insectno then do;
		in0=insectno; TmBegPrbFrstE=0;
	end;
	TmBegPrbFrstE=TmBegPrbFrstE+dur;
data three; set three; drop in0;
data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	else marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; drop waveform dur line sumstart sumend instance inverter1 in0 marker1;
data Ebert; set Ebert three; merge Ebert three; by insectno;

**********************************************************************
*  Finding Time from start of probe with first E to 1st E is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
********************************   Start New Method        **********
**	Finding the number of G waveforms                      **********
**	Finding the time spent in the G waveform               **********
**	Finding the average duration of each G waveform event  **********
*********************************************************************;
data three; set OnlyG;
proc sort; by insectno waveform;
data three; set three; drop line sumstart sumend instance inverter1;
data three; set three;
proc means noprint; by insectno waveform;  output out=outsas n=num mean=avg sum=sum1;
data G; set outsas; if compress(upcase(waveform))='G' then output;
Data G; set G; NumG=num; DurG=sum1; MeanG=avg; drop _TYPE_ _FREQ_ waveform num avg sum1;
Data Ebert; set Ebert g; merge Ebert g; by insectno;
data Ebert; set Ebert; if NumG='.' then NumG=0;
proc datasets nolist nodetails; delete G outsas three;
*********************************************************************
*  Finding NumG DurG, and MeanG is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
**	Number of Probes after first E1
*********************************************************************;
Data three; set OnlyE1;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='E1' then marker1=1;
data three; set three; if marker1=1 then output;
data three; set three; drop in0 marker1;

data three; set three;
	retain in0 marker2 delay1;
	if in0 ne insectno then do;
		in0=insectno; marker2=0; delay1=1;
	end;
	if delay1=0 then do;
		if compress(upcase(waveform))='Z' then marker2=marker2+1;
		if compress(upcase(waveform))='NP' then marker2=marker2+1;
		 
	end;
	delay1=0;
data three; set three; if compress(upcase(waveform))='C' then output;
data three; set three; proc means noprint;
	by insectno; var marker2; output out=outsas max=NumPrbsAftrFrstE;
data outsas; set outsas; if NumPrbsAftrFrstE='.' then NumPrbsAftrFrstE=0;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
Data Ebert; set Ebert; if NumPrbsAftrFrstE='.' then NumPrbsAftrFrstE=0;
proc datasets nodetails nolist; delete three outsas;
run;
*********************************************************************
*  Finding Number of probes after first E1 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
********************************   Start New Method    **************
**	Number of Probes<3min after first E1
*********************************************************************;
Data three; set OnlyE1;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='E1' then marker1=1;
data three; set three; if marker1=1 then output;
data three; set three; drop in0 marker1;

Data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='NP' or compress(upcase(waveform))="Z" then marker1=1;
data three; set three; if marker1=1 then output;
data three; set three; drop in0 marker1;
data three; set three;
	retain in0 marker2 delay1;
	if in0 ne insectno then do;
		in0=insectno; marker2=1; delay1=1;
	end;
	if delay1=0 then do;
		if compress(upcase(waveform))='Z' then marker2=marker2+1;
		if compress(upcase(waveform))='NP' then marker2=marker2+1;
		 
	end;
	delay1=0;
Data three; set three; if compress(upcase(waveform)) ne "Z" and compress(upcase(waveform)) ne "NP" then waveform="PRB";
data three; set three; 
  proc sort; by insectno marker2 waveform;
  proc means noprint; by insectno marker2 waveform; var dur; output out=outsas2 sum=sdur;
data outsas2; set outsas2; if waveform="PRB" and sdur<180 then output;
data outsas2; set outsas2;
	retain in0 marker1;
	if in0 ne insectno then do;	in0=insectno; marker1=0; end;
	marker1=marker1+1;
Data outsas2; set outsas2;
  proc means noprint; by insectno; var marker1; output out=outsas4 max=NmbrShrtPrbAftrFrstE;

data outsas4; set outsas4; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas4; merge Ebert outsas4; by insectno;
Data Ebert; set Ebert; if NmbrShrtPrbAftrFrstE='.' then NmbrShrtPrbAftrFrstE=0;
proc datasets nolist nodetails; delete three outsas4 outsas2;
run;
*********************************************************************
*  Finding Number of probes <3min after first E1 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;



*********************************************************************
*********************************   Start New Method    *************
***	Number of E1
*********************************************************************;
Data three; set onlye1;
data three; set three; if compress(upcase(waveform))='E1' then output;
data three; set three;
marker1=1;
data three; set three; proc means noprint;
by insectno; var marker1; output out=outsas sum=NumE1;
data outsas; set outsas; Drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
data Ebert; set Ebert; if NumE1='.' then NumE1=0;
*********************************************************************
*  Finding number of E1 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
*** Number of E1 (longer than 10 min) followed by E2
*********************************************************************;
Data three; set onlye1;
data three; set three;
	retain in0 w1 w0 holder1 marker1;
	w1=compress(upcase(waveform));
	if in0 ne insectno then do;
		in0=insectno; w0='   '; holder1=0; marker1=0;
	end;
	if w0='E1' and holder1>600 and w1='E2' then marker1=marker1+1;
	w0=w1;
	holder1=dur;
data three; set three; proc means noprint; 
	by insectno; var marker1; output out=outsas max=NumLngE1BfrE2;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
data Ebert; set Ebert; if NumLngE1BfrE2='.' then NumLngE1BfrE2=0;
*********************************************************************
*  Finding Number of E1 (longer than 10 min) followed by E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*********************************************************************
*********************************   Start New Method    *************
***   Number of single E1
*********************************************************************;
data three; set onlyE1;
	retain in0 w1 w0 marker1 marker2 marker3;
	w1=compress(upcase(waveform));
	if in0 ne insectno then do;
		in0=insectno; w0='   '; marker1=0; marker2=0; marker3=0;
	end;
	if w1 ne 'E1' and w1 ne 'E2' then marker1=1; else marker1=0;
	if w0='E2' and w1='E1' then marker3=1; else marker3=0;
	if marker1=1 and w0='E1' and marker3=0 then marker2=marker2+1;
	w0=w1;
data three; set three; drop in0 w1 w0 marker1;
data three; set three;
	retain in0 marker1 holder1 holder2;
	if in0 ne insectno then do;
		in0=insectno; marker1=0; holder1=0; holder2=0;
	end;
	if holder1 ne marker2 and holder2=1 then marker1=marker1+1;
	holder1=marker2;
	holder2=marker3;
data three; set three; marker4=marker2-marker1;
data three; set three; proc means noprint; 
	by insectno; var marker4; output out=outsas max=NumSnglE1;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
data Ebert; set Ebert; if NumSnglE1='.' then NumSnglE1=0;
run;
*********************************************************************
*  Find Number of single E1 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
***  Finding number of E2
*********************************************************************;
Data three; set OnlyE2;
if compress(upcase(waveform))='E2' then marker1=1; else marker1=0;
data three; set three; proc means noprint;
	by insectno; var marker1; output out=outsas sum=NumE2;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
*********************************************************************
*  Finding Number of E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
***   Find Number of sustained E2;
*********************************************************************;
Data three; set OnlyE2;
if compress(upcase(waveform))='E2' and dur>600 then marker1=1; else marker1=0;
data three; set three; proc means noprint;
	by insectno; var marker1; output out=outsas sum=NumLngE2;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
********************************************************************
*  Finding Number of sustained E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Find Duration of first E (E1 + E2)
*********************************************************************;
Data three; set onlyE1;
	retain marker1 w0 w1 in0;
	if in0 ne insectno then do; w0="    "; in0=insectno; marker1=0; end;
	w1=compress(upcase(waveform));
	if w0="E2" then marker1=1;	
	w0=w1;
Data three; set three; if marker1=0 then output;
Data three; set three; drop w0 w1 in0 marker1;

Data three; set three;
	if compress(upcase(waveform))='E1' then waveform='E';
	if compress(upcase(waveform))='E2' then waveform='E';
data three; set three;
	retain sort1 w0 in0;
	if in0 ne insectno then do; sort1=1; in0=insectno; end;
	w1=compress(upcase(waveform));
	if w1 ne w0 then sort1=sort1+1; else sort1=sort1;
	w0=w1;
data three; set three;
proc sort; by insectno waveform;
proc means noprint; by insectno waveform sort1; var dur; output out=outthree sum=durs;
data outthree; set outthree;
data three; set outthree; drop _TYPE_ _FREQ_;
data three; set three;
proc sort; by insectno waveform;
data three;set three; by insectno waveform;
	retain instance;
	if first.waveform then instance=0;
	instance=instance+1;
data three; set three; if waveform='E' and instance=1 then output;
proc datasets nolist nodetails; delete outthree;
data three; set three;
DurFirstE=durs;
drop instance sort1 waveform durs;
data Ebert; set Ebert three; merge Ebert three; by insectno;
*********************************************************************
*  Finding duration of first E is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*********************************************************************
*********************************   Start New Method    *************
****	Find contribution of E1 to phloem phase
*********************************************************************;
Data three; set onlye1;
	if compress(upcase(waveform))='E1' or compress(upcase(waveform))='E2' then output;
Data three; set three;
	proc sort; by insectno waveform;
	Proc means noprint; by insectno waveform; var dur; output out=outsas sum=outSum;
data four; set outsas; if compress(upcase(waveform))='E1' then output;
data four; set four; outsumE1=outsum; drop outsum waveform _TYPE_ _FREQ_;
data five; set outsas; if compress(upcase(waveform))='E2' then output;
data five; set five; outsumE2=outsum; drop outsum waveform _TYPE_ _FREQ_;
data three; set four five; merge four five; by insectno;
data three; set three;
if outsume1='.' then outsume1=0;
if outsume2='.' then outsume2=0;
data three; set three;
ttlsum=outsumE1+outsumE2;
CntrbE1toE=100*(outsumE1/ttlsum);
data three; set three; drop outsumE1 outsumE2 ttlsum;
data Ebert; set Ebert three; merge Ebert three; by insectno;
Proc datasets nolist nodetails; delete three four five outsas;
*********************************************************************
*  Finding contribution of E1 to phloem phase is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*********************************************************************
*********************************   Start New Method    *************
****	Duration of E1 followed by first sustained E2 (Long E2)
*********************************************************************;
data three; set onlysuse2;
	retain in0 marker1;
	if in0 ne insectno then do;	in0=insectno; marker1=0; end;
	if compress(upcase(waveform))='E2' and dur>600 then marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; drop in0 marker1;
data three; set three; proc sort; by insectno inverter1;
data three; set three; if compress(upcase(waveform))='E1' then output;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;	in0=insectno; marker1=0; end;
	else marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; 	DurE1FlwdFrstSusE2 =dur;
Data three; set three;	drop waveform line dur sumstart sumend instance inverter1 in0 marker1;
data Ebert; set Ebert three; merge Ebert three; by insectno;
Proc datasets nolist nodetails; delete three;
*********************************************************************
*  Finding Duration of E1 followed by first E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*********************************************************************
*********************************   Start New Method    *************
****	Duration of E1 followed by sustained E2
*********************************************************************;
data three; set onlye2;
	retain in0 marker1;
	if in0 ne insectno then do;	in0=insectno; marker1=0; end;
	if compress(upcase(waveform))='E2' then marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; drop in0 marker1;
data three; set three; proc sort; by insectno inverter1;
data three; set three; if compress(upcase(waveform))='E1' then output;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;	in0=insectno; marker1=0; end;
	else marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; 	DurE1FlldFrstE2 =dur;
Data three; set three;	drop waveform line dur sumstart sumend instance inverter1 in0 marker1;
data Ebert; set Ebert three; merge Ebert three; by insectno;
Proc datasets nolist nodetails; delete three;
*********************************************************************
*  Finding Duration of E1 followed by sustained E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
**************  END OF PART 2   *************************************
*********************************************************************;


********************************************************************
*********************************   Start New Method    ************
****	Potential E2 Index
********************************************************************;
Data three; set onlyE2;
data three; set three;
	retain in0 w1 w0 marker2;
	w1=Compress(upcase(waveform));
	if insectno ne in0 then do;
	  w0='  '; marker2=0;
	 in0=insectno; 
	end;
	if compress(upcase(waveform))='E2' then marker2=1;
data three; set three; if marker2=1 then output;
data three; set three; proc means noprint; by insectno; var dur; output out=outsas sum=DurE2toEnd;
data three; set three; if compress(upcase(waveform))='E2' then output;
data three; set three; proc means noprint; by insectno; var dur; output out=outsas1 sum=DurAllE2;
data outsas; set outsas outsas1; merge outsas outsas1; by insectno;
data outsas; set outsas; PotE2Indx=100*(DurAllE2/DurE2toEnd);
data outsas; set outsas; drop _TYPE_ _FREQ_ DurAllE2 DurE2toEnd;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nolist nodetails; delete outsas1 three outsas;
*********************************************************************
*  Finding Potential E2 Index is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Find total duration of E
*********************************************************************;
Data three; set onlye1;
if compress(upcase(waveform))='E1' or compress(upcase(waveform))='E2' then output;
data three; set three; proc means noprint; by insectno; var dur; output out=outsas sum=TtlDurE;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
*********************************************************************
*  Finding total duration of E is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Find total duration of E1
*********************************************************************;
Data three; set onlye1;
if compress(upcase(waveform))='E1' then output;
data three; set three; proc means noprint; by insectno; var dur; output out=outsas sum=TtlDurE1;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
Proc datasets nolist nodetails; delete three outsas;
*********************************************************************
*  Finding total duration of E1 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Find Total Duration of E1 followed by a sustained E2
*********************************************************************;
data three; set onlysusE2;
	if compress(upcase(waveform))='E2' and dur>600 then marker1=1;
	else marker1=0;
data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain in0 w0 marker2;
	w1=compress(upcase(waveform));
	if in0 ne insectno then do;
		in0=insectno; marker2=0; 
	end;
	if w1="E1" and w0=1 then marker2=1; else marker2=0;
	w0=marker1;
Data three; set three; proc sort; by insectno line;
data three; set three; if marker2=1 then output;
data three; set three; proc means noprint; by insectno; var dur; output out=outsas sum=TtlDurE1FlldSusE2;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
Proc datasets nolist nodetails; delete three outsas;

*********************************************************************
*  Finding Total Duration of E1 followed by a sustained E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*********************************************************************
*********************************   Start New Method    *************
****	Total duration of E1 followed by E2
*********************************************************************;
Data three; set onlye2; proc sort; by insectno inverter1;
data three; set three;
	retain marker1 in0 w1 w0;
	w1=compress(upcase(waveform));
	if in0 ne insectno then do;
		in0=insectno; marker1=0; w0='   ';
	end;
	if w0='E2' and w1='E1' then marker1=1; else marker1=0;
	w0=w1;
data three; set three; drop in0;
data three; set three; if marker1=1 then output;
data three; set three; proc means noprint;
	by insectno; var dur; output out=outsas sum=TtlDurE1FlldE2;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
*********************************************************************
*  Finding Total duration of E1 followed by E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Total duration of single E1
*********************************************************************;
Data three; set OnlyE1;
Data three; Set three; proc sort; by insectno inverter1;
data three; set three;
	retain w0 w1 in0 marker2;
	w1=Compress(upcase(waveform));
	if insectno ne in0 then do;
	  w0='  '; marker1=0;
	 in0=insectno; 
	end;
	if w1='E1' and w0 ne 'E2' then marker1=1;
	w0=w1;
data three; set three; proc sort; by insectno line;
data three; set three; drop in0 w1 w0;
data three; set three;
	retain in0 w0 w1;
	w1=compress(upcase(waveform));
	if insectno ne in0 then do;
		in0=insectno; w0='   ';
	end;
	if w0='E2' and w1='E1' then marker1=0;
	w0=w1;
data three; set three; if marker1=1 then output;
data three; set three; proc means noprint;
	by insectno; var dur; output out=outsas sum=TtlDurSnglE1;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
*********************************************************************
*  Finding total duration of single e1 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Total duration of E1 followed by E2 plus E2
*********************************************************************;
Data three; set onlye2; proc sort; by insectno inverter1;
data three; set three;
	retain marker1 in0 w1 w0;
	w1=compress(upcase(waveform));
	if in0 ne insectno then do;
		in0=insectno; marker1=0; w0='   ';
	end;
	if w0='E2' and w1='E1' then marker1=1; else marker1=0;
	w0=w1;
data three; set three; drop in0;
data three; set three; if marker1=1 then output;
data three; set three; proc means noprint;
	by insectno; var dur; output out=outsas sum=TtlDurE1FlldE2;
data outsas; set outsas; drop _TYPE_ _FREQ_;

data three; set onlye2; if compress(upcase(waveform))='E2' then output;
data three; set three; proc means noprint; 
	by insectno; var dur; output out=outsas2 sum=SE2;
data outsas2; set outsas2; drop _TYPE_ _FREQ_;
data three; set outsas outsas2; merge outsas outsas2; by insectno;
data three; set three;
TtlDurE1FllwdE2PlsE2=TtlDurE1FlldE2+SE2;
data three; set three; drop se2 TtlDurE1FlldE2;
data Ebert; set Ebert three; merge Ebert three; by insectno;
proc datasets nolist nodetails; delete outsas outsas2 three;
*********************************************************************
*  Finding Total duration of E1 followed by E2 plus E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Total Duration of E2
*********************************************************************;
data three; set onlye2;
	if compress(upcase(waveform))='E2' then output;
data three; set three; proc means noprint;
	by insectno; var dur; output out=outsas sum=TtlDurE2;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
*********************************************************************
*  Finding Total duration of E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Mean Duration of E1
*********************************************************************;
data three; set onlye1;
	if compress(upcase(waveform))='E1' then output;
data three; set three; proc means noprint;
	by insectno; var dur; output out=outsas mean=MnDurE1;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
*********************************************************************
*  Finding Mean duration of E1 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Mean duration of E2
*********************************************************************;
data three; set onlye2;
	if compress(upcase(waveform))='E2' then output;
data three; set three; proc means noprint;
	by insectno; var dur; output out=outsas mean=MnDurE2;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
*********************************************************************
*  Finding mean duration of E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Number of probes
*********************************************************************;
data three; set one;
	retain in0 marker1;
	w1=compress(upcase(waveform));
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if w1='C' then marker1=1;
	if w1='Z' or w1='NP' then marker1=0;
data three; set three; drop in0;
data three; set three;
	retain in0 marker2 holder1;
	if in0 ne insectno then do;
		in0=insectno; marker2=0; holder1=0;
	end;
	if holder1=0 and marker1=1 then marker2=marker2+1;
	holder1=marker1;
data three; set three; proc means noprint;
	by insectno; var marker2; output out=outsas max=NumPrbs;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
*********************************************************************
*  Finding number of probes is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Find number of C events
*********************************************************************;
data three; set One; if compress(upcase(waveform))='C' then output;
Data three; set three; marker1=1;
proc means noprint;
	by insectno; var marker1; output out=outsas sum=NmbrC;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
*********************************************************************
*  Finding number of C is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Find number of short C events
*********************************************************************;
data three; set one;
Data three; set three; 
retain in0 marker1;
if in0 ne insectno then do;
in0=insectno; marker1=0;
end;
if compress(upcase(waveform))="C" then marker1=1;
if compress(upcase(waveform))="NP" or compress(upcase(waveform))="Z" then marker1=0;
data three; set three; drop in0;
data three; set three; 
retain in0 marker2 holder1;
if in0 ne insectno then do;
in0=insectno; marker2=0;
end;
if holder1=0 and marker1=1 then marker2=marker2+1;
holder1=marker1;
data three; set three; if compress(upcase(waveform)) ne "NP" and compress(upcase(waveform)) ne "Z" then output;
data three; set three; proc means noprint; var dur; by insectno marker2; output out=outsas sum=dur2;
data three; set outsas; if dur2<180 then output; drop _TYPE_ _FREQ_;
data three; set three;
retain in0 marker3;
if in0 ne insectno then do;
in0=insectno; marker3=0;
end;
marker3=marker3+1;
data three; set three; proc means noprint; by insectno; var marker3; output out=outsas max=NmbrShrtC;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
*********************************************************************
*  Finding number of short C is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Find the Number of NP
*********************************************************************;
Data three; set one;
data three; set three; if compress(upcase(waveform))="NP" or compress(upcase(waveform))="Z" then marker1=1; else marker1=0;
data three; set three; proc means noprint; by insectno; var marker1; output out=outsas sum=NumNP;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nolist nodetails; delete outsas three;
run;
*********************************************************************
*  Finding ........... is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
****** END Part 3  **************************************************
*********************************************************************;



*********************************************************************
*********************************   Start New Method    *************
****	Find the Total duration of C
*********************************************************************;
Data three; set one;
data three; set three; if compress(upcase(waveform))="C" then output;
data three; set three; proc means noprint; by insectno; var dur; output out=outsas sum=TtlDurC;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
run;
*********************************************************************
*  Finding the Total duration of C is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*********************************************************************
*********************************   Start New Method    *************
****	Find the Total duration of non-phloematic phase
*********************************************************************;
Data three; set OnlyE1;
data three; set three; if compress(upcase(waveform))ne "E1" and compress(upcase(waveform)) ne "E2" then output;
data three; set three; proc means noprint; by insectno; var dur; output out=outsas sum=TotDurNnPhlPhs;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
run;
*********************************************************************
*  Finding the Total duration of non-phloematic phase is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*********************************************************************
*********************************   Start New Method    *************
****	Find the Total duration of NP phase
*********************************************************************;
Data three; set one; 
if compress(upcase(waveform))eq "NP" or compress(upcase(waveform)) eq "Z" then output;
Data three; set three;
proc means noprint; var dur; by insectno; output out=outsas sum=TtlDurNP;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
*********************************************************************
*  Finding the Total duration of NP phase is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;



*********************************************************************
*********************************   Start New Method    *************
****	Find the Total probing time
*********************************************************************;
Data three; set one; 
retain in0 marker1;
w0=compress(upcase(waveform));
if in0 ne insectno then do; in0=insectno; marker1=0; end;
If w0="C" then marker1=1;
if w0="NP" or w0="Z" then marker1=0;
data three; set three; if marker1=1 then output;
proc means noprint; var dur; by insectno; output out=outsas sum=TtlPrbTm;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
*********************************************************************
*  Finding the Total probing time is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Find the mean duration of NP
*********************************************************************;
Data three; set one; 
if compress(upcase(waveform))="NP" or compress(upcase(waveform)) eq "Z" then output;
data three; set three;
proc means noprint; var instance dur; by insectno; output out=outsas sum=Ins1 dur1 max=InsM durM;
data outsas; set outsas; MnDurNP=dur1/InsM;
data outsas; set outsas; drop ins1 dur1 insm durm _FREQ_ _TYPE_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
*********************************************************************
*  Finding the mean duration of NP is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;



*********************************************************************
*********************************   Start New Method    *************
****	Find the mean duration of C
*********************************************************************;
Data three; set one; 
if compress(upcase(waveform))="C" then output;
data three; set three;
proc means noprint; var instance dur; by insectno; output out=outsas sum=Ins1 dur1 max=InsM durM;
data outsas; set outsas; MnDurC=dur1/InsM;
data outsas; set outsas; drop ins1 dur1 insm durm _FREQ_ _TYPE_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
*********************************************************************
*  Finding the mean duration of C is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****	Find time to first sustained E2
*********************************************************************;
Data three; set OnlySusE2; 
retain in0 marker1;
if in0 ne insectno then do; in0=insectno; marker1=0; end;
if compress(upcase(waveform))="E2" and dur>600 then marker1=1;
data three; set three; if marker1=0 then output;
data three; set three;
proc means noprint; var sumend; by insectno; output out=outsas max=TmSusE2;
data four; set one;
proc means noprint; var sumend; by insectno; output out=outsas1 max=runtime;
data outsas; set outsas outsas1; merge outsas outsas1; by insectno;
* if tmsuse2="." then TmFrstSusE2=runtime; *else TmFrstSusE2=TmSusE2; * This line makes output match Sarria, and it has been changed;
TmFrstSusE2=TmSusE2;
data outsas; set outsas; drop runtime TmSusE2 _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete outsas outsas1 four;
run;
*********************************************************************
*  Finding the time to the first sustained E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*****  END PART 4  **************************************************
*********************************************************************;


*********************************************************************
*********************************   Start New Method    *************
*************     Time to first sustained E2 from first probe   *****
*********************************************************************;
Data three; set OnlySusE2; 
retain in0 marker1;
if in0 ne insectno then do; in0=insectno; marker1=0; end;
if compress(upcase(waveform))="E2" and dur>600 then marker1=1;
data three; set three; if marker1=0 then output;
data three; set three;
proc means noprint; var sumend; by insectno; output out=outsas max=TmSusE2;
data four; set one;
proc means noprint; var sumend; by insectno; output out=outsas1 max=runtime;
data outsas; set outsas outsas1; merge outsas outsas1; by insectno;
TmFrstSusE2=TmSusE2;
data outsas; set outsas; drop runtime tmsuse2 _TYPE_ _FREQ_;
data three; set one; retain marker1;
data three; set three;
retain in0 marker1;
if in0 ne insectno then do; in0=insectno; marker1=0; end;
if compress(upcase(waveform))="C" then marker1=1;
Data three; set three; If marker1=0 then output;
data three; set three; drop in0 marker1;
Data three; set three;
proc means noprint; var dur; by insectno; output out=outsas3 sum=Sumdur;
Data outsas3; set outsas3; drop _TYPE_ _FREQ_;
data outsas; set outsas outsas3; merge outsas outsas3; by insectno;
data outsas; set outsas; TmFrstSusE2FrstPrb=TmfrstSusE2-Sumdur;
data outsas; set outsas; if TmFrstSusE2FrstPrb<=0 then TmFrstSusE2FrstPrb=".";
data outsas; set outsas; drop TmfrstSusE2 Sumdur;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four outsas outsas1 outsas3 three;
*********************************************************************
*  Finding first sustained E2 from first probe is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
********************   Cleaning up a bit ****************************;
Data Ebert; set Ebert; drop dur0;

*********************************************************************
*********************************************************************
*********************************   Start New Method    *************
*************     Time to first sustained E2 from start of probe   **
*********************************************************************;
data three; set OnlySusE2;
retain marker1;
if compress(upcase(waveform))="C" then marker1=1;
if compress(upcase(waveform))="NP" or compress(upcase(waveform))="Z" then marker1=0;
Data three; set three;
retain marker2;
if compress(upcase(waveform))="E2" and dur>600 then marker2=1;
if compress(upcase(waveform))="NP" or compress(upcase(waveform))="Z" then marker2=0;
data four; set three;
proc sort; by inverter1;
data four; set four;
retain marker3 in0;
if in0 ne insectno then do; in0=insectno; marker3=0; end;
if marker2=1 then marker3=1;
if compress(upcase(waveform))="NP" or compress(upcase(waveform))="Z" then marker3=0;
data four; set four;
proc sort; by line;
data four; set four;
if marker3=1 then output;
data four; set four;
drop marker1 marker2 marker3 in0;
data four; set four;
retain marker1 in0;
if in0 ne insectno then do; in0=insectno; marker1=0; end;
if compress(upcase(waveform))="E2" and dur>600 then marker1=1;
data four; set four;
if marker1=0 then output;
data four; set four;
proc means noprint; var dur; by insectno; output out=outsas sum=TmFrstSusE2StrtPrb;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete three four outsas;
*********************************************************************
*  Finding Time to first sustaines E2 from start of probe is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
**************       Time to first E2 from start of EPG
*********************************************************************;
data three; set OnlyE2;
retain marker1 in0;
if in0 ne insectno then do; marker1=0; in0=insectno; end;
if compress(upcase(waveform))="E2" then marker1=1;
data three; set three;
if marker1=0 then output;
proc means noprint; var sumend; by insectno; output out=outsas max=TmFrstE2StrtEPG;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
Proc datasets nodetails nolist; delete three outsas;
*********************************************************************
*  Finding time to first E2 from start of EPG is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
**************       Time to first E2 from first probe
*********************************************************************;
data three; set OnlyE2;
retain marker1 in0;
if in0 ne insectno then do; marker1=0; in0=insectno; end;
if compress(upcase(waveform))="E2" then marker1=1;
data three; set three;
if marker1=0 then output;
proc means noprint; var sumend; by insectno; output out=outsas max=result1;

data three; set three; drop in0 marker1;
data three; set one; retain marker1;
data three; set three;
retain in0 marker1;
if in0 ne insectno then do; in0=insectno; marker1=0; end;
if compress(upcase(waveform))="C" then marker1=1;
Data three; set three; If marker1=0 then output;
data three; set three; drop in0 marker1;
Data three; set three;
proc means noprint; var dur; by insectno; output out=outsas3 sum=Sumdur;
Data outsas3; set outsas3; drop _TYPE_ _FREQ_;
data outsas; set outsas outsas3; merge outsas outsas3; by insectno;


data outsas; set outsas; TmFrstE2FrmFrstPrb=result1-Sumdur; drop result1 Sumdur _TYPE_ _FREQ_;
data outsas; set outsas; if TmFrstE2FrmFrstPrb<=0 then TmFrstE2FrmFrstPrb=".";
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
Proc datasets nodetails nolist; delete three four outsas3 outsas;
*********************************************************************
*  Finding time to first E2 from first probe is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
*************     Time to first E2 from start of probe   ************
*********************************************************************;
data three; set OnlyE2;
retain marker1;
if compress(upcase(waveform))="C" then marker1=1;
if compress(upcase(waveform))="NP" or compress(upcase(waveform))="Z" then marker1=0;
Data three; set three;
retain marker2;
if compress(upcase(waveform))="E2" then marker2=1;
if compress(upcase(waveform))="NP" or compress(upcase(waveform))="Z" then marker2=0;
data four; set three;
proc sort; by inverter1;
data four; set four;
retain marker3 in0;
if in0 ne insectno then do; in0=insectno; marker3=0; end;
if marker2=1 then marker3=1;
if compress(upcase(waveform))="NP" or compress(upcase(waveform))="Z" then marker3=0;
data four; set four;
proc sort; by line;
data four; set four;
if marker3=1 then output;
data four; set four;
drop marker1 marker2 marker3 in0;
data four; set four;
retain marker1 in0;
if in0 ne insectno then do; in0=insectno; marker1=0; end;
if compress(upcase(waveform))="E2" then marker1=1;
data four; set four;
if marker1=0 then output;
data four; set four;
proc means noprint; var dur; by insectno; output out=outsas sum=TmFrstE2FrmPrbStrt;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete three four outsas;
*********************************************************************
*  Finding Time to first E2 from start of probe is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
***********      Duration of NP by hour
*********************************************************************;
Data three; set one;
if compress(upcase(waveform))="NP" then output;
data four; set three;
retain ttldur in0 marker4;
if in0 ne insectno then do; in0=insectno; ttldur=0; marker4=0; end;
if sumend<=3600 then do; ttldur=ttldur+dur; marker4=1; end;
if sumstart<=3600 and sumend>3600 and marker4=0 then ttldur=ttldur+(3600-sumstart);
if sumstart=0 and sumend>3600 then ttldur=3600;
marker4=0;
data four; set four;
proc means noprint; var ttldur; by insectno; output out=outsas max=TtlDurNp1;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four;

data four; set three;
retain ttldur in0 marker4;
if in0 ne insectno then do; in0=insectno; ttldur=0; marker4=0; end;
if sumend-3600>0 and sumstart<=3600 then do; ttldur= sumend-3600; marker4=1; end;
if sumstart>3600 and sumend<=7200 and marker4=0 then do; ttldur=ttldur+dur; marker4=1; end;
if sumstart<=7200 and sumend>7200 and marker4=0 then ttldur=ttldur+(7200-sumstart);
if sumstart<3600 and sumend>7200 then ttldur=3600;
marker4=0;
data four; set four;
proc means noprint; var ttldur; by insectno; output out=outsas max=TtlDurNp2;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four;

data four; set three;
retain ttldur in0 marker4;
if in0 ne insectno then do; in0=insectno; ttldur=0; marker4=0; end;
if sumend-7200>0 and sumstart<=7200 then do; ttldur= sumend-7200; marker4=1; end;
if sumstart>7200 and sumend<=10800 and marker4=0 then do; ttldur=ttldur+dur; marker4=1; end;
if sumstart<=10800 and sumend>10800 and marker4=0 then ttldur=ttldur+(10800-sumstart);
if sumstart<7200 and sumend>10800 then ttldur=3600;
marker4=0;
data four; set four;
proc means noprint; var ttldur; by insectno; output out=outsas max=TtlDurNp3;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four;

data four; set three;
retain ttldur in0 marker4;
if in0 ne insectno then do; in0=insectno; ttldur=0; marker4=0; end;
if sumend-10800>0 and sumstart<=10800 then do; ttldur= sumend-10800; marker4=1; end;
if sumstart>10800 and sumend<=14400 and marker4=0 then do; ttldur=ttldur+dur; marker4=1; end;
if sumstart<=14400 and sumend>14400 and marker4=0 then ttldur=ttldur+(14400-sumstart);
if sumstart<10800 and sumend>14400 then ttldur=3600;
marker4=0;
data four; set four;
proc means noprint; var ttldur; by insectno; output out=outsas max=TtlDurNp4;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four;

data four; set three;
retain ttldur in0 marker4;
if in0 ne insectno then do; in0=insectno; ttldur=0; marker4=0; end;
if sumend-14400>0 and sumstart<=14400 then do; ttldur= sumend-14400; marker4=1; end;
if sumstart>14400 and sumend<=18000 and marker4=0 then do; ttldur=ttldur+dur; marker4=1; end;
if sumstart<=18000 and sumend>18000 and marker4=0 then ttldur=ttldur+(18000-sumstart);
if sumstart<14400 and sumend>18000 then ttldur=3600;
marker4=0;
data four; set four;
proc means noprint; var ttldur; by insectno; output out=outsas max=TtlDurNp5;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four;

data four; set three;
retain ttldur in0 marker4;
if in0 ne insectno then do; in0=insectno; ttldur=0; marker4=0; end;
if sumend-18000>0 and sumstart<=18000 then do; ttldur= sumend-18000; marker4=1; end;
if sumstart>18000 and sumend<=21600 and marker4=0 then do; ttldur=ttldur+dur; marker4=1; end;
if sumstart<=21600 and sumend>21600 and marker4=0 then ttldur=ttldur+(21600-sumstart);
if sumstart<18000 and sumend>21600 then ttldur=3600;
marker4=0;
data four; set four;
proc means noprint; var ttldur; by insectno; output out=outsas max=TtlDurNp6;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four;

Data ebert; set ebert;
if TtlDurNP1="." then TtlDurNP1=0;
if TtlDurNP2="." then TtlDurNP2=0;
if TtlDurNP3="." then TtlDurNP3=0;
if TtlDurNP4="." then TtlDurNP4=0;
if TtlDurNP5="." then TtlDurNP5=0;
if TtlDurNP6="." then TtlDurNP6=0;

Run;
*********************************************************************
*  Finding duration of NP by hour is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*********************************************************************
*********************************   Start New Method    *************
***********      Number of probes by hour
*********************************************************************;
Data three; set one;
retain marker1 marker2 in0;
w1=compress(upcase(waveform));
if in0 ne insectno then do; in0=insectno; marker1=0; marker2=0; end;
if w1="C" then marker1=1;
if w1="NP" or w1="Z" then marker2=1; else marker2=0;
if marker1=1 and marker2=1 then marker1=0;
Data three; set three; drop marker2 in0;
data three; set three;
retain marker2 marker3 in0;
if in0 ne insectno then do; in0=insectno; marker2=0; marker3=0; end;
if marker2 ne marker1 then do; marker2=marker1; marker3=marker3+1; end;
data three; set three;
proc means noprint; var dur; by insectno marker3; output out=outsas sum=durProbe;
data outsas; set outsas; if mod(marker3,2) = 1 then waveform="X "; else waveform="NP";
data outsas; set outsas;
retain sumstart sumend in0 holder1;
if in0 ne insectno then do; in0=insectno; sumstart=0; sumend=0; end;
if sumend ne 0 then sumstart=sumstart+holder1;
sumend=sumend+durprobe;
holder1=durprobe;

data three; set outsas;
drop _TYPE_ _FREQ_ in0 holder1;
data three; set three;
if waveform="X " then output;
data four; set three;
retain ttl1 in0 mark4;
if in0 ne insectno then do; in0=insectno; ttl1=0; mark4=0; end;
if sumend<=3600 then do; ttl1=ttl1+1; mark4=1; end;
if sumstart<3600 and sumend>3600 and mark4=0 then ttl1=ttl1+1;
mark4=0;
data four; set four;
proc means noprint; var ttl1; by insectno; output out=outsas max=NumPrb1;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four outsas;

data four; set three;
retain ttl1 in0 mark4;
if in0 ne insectno then do; in0=insectno; ttl1=0; mark4=0; end;
if sumend-3600>0 and sumstart<=3600 then do; ttl1= ttl1+1; mark4=1; end;
if sumstart>3600 and sumend<=7200 and mark4=0 then do; ttl1=ttl1+1; mark4=1; end;
if sumstart<7200 and sumend>7200 and mark4=0 then ttl1=ttl1+1;
mark4=0;
data four; set four;
proc means noprint; var ttl1; by insectno; output out=outsas max=NumPrb2;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four outsas;

data four; set three;
retain ttl1 in0 mark4;
if in0 ne insectno then do; in0=insectno; ttl1=0; mark4=0; end;
if sumend-7200>0 and sumstart<=7200 then do; ttl1= ttl1+1; mark4=1; end;
if sumstart>7200 and sumend<=10800 and mark4=0 then do; ttl1=ttl1+1; mark4=1; end;
if sumstart<10800 and sumend>10800 and mark4=0 then ttl1=ttl1+1;
mark4=0;
data four; set four;
proc means noprint; var ttl1; by insectno; output out=outsas max=NumPrb3;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four outsas;

data four; set three;
retain ttl1 in0 mark4;
if in0 ne insectno then do; in0=insectno; ttl1=0; mark4=0; end;
if sumend-10800>0 and sumstart<=10800 then do; ttl1= ttl1+1; mark4=1; end;
if sumstart>10800 and sumend<=14400 and mark4=0 then do; ttl1=ttl1+1; mark4=1; end;
if sumstart<14400 and sumend>14400 and mark4=0 then ttl1=ttl1+1;
mark4=0;
data four; set four;
proc means noprint; var ttl1; by insectno; output out=outsas max=NumPrb4;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four outsas;

data four; set three;
retain ttl1 in0 mark4;
if in0 ne insectno then do; in0=insectno; ttl1=0; mark4=0; end;
if sumend-14400>0 and sumstart<=14400 then do; ttl1= ttl1+1; mark4=1; end;
if sumstart>14400 and sumend<=18000 and mark4=0 then do; ttl1=ttl1+1; mark4=1; end;
if sumstart<18000 and sumend>18000 and mark4=0 then ttl1=ttl1+1;
mark4=0;
data four; set four;
proc means noprint; var ttl1; by insectno; output out=outsas max=NumPrb5;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four outsas;

data four; set three;
retain ttl1 in0 mark4;
if in0 ne insectno then do; in0=insectno; ttl1=0; mark4=0; end;
if sumend-18000>0 and sumstart<=18000 then do; ttl1=ttl1+1; mark4=1; end;
if sumstart>18000 and sumend<=21600 and mark4=0 then do; ttl1=ttl1+1; mark4=1; end;
if sumstart<21600 and sumend>21600 and mark4=0 then ttl1=ttl1+1;
mark4=0;
data four; set four;
proc means noprint; var ttl1; by insectno; output out=outsas max=NumPrb6;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four outsas;

Data ebert; set ebert;
if NumPrb1="." then NumPrb1=0;
if NumPrb2="." then NumPrb2=0;
if NumPrb3="." then NumPrb3=0;
if NumPrb4="." then NumPrb4=0;
if NumPrb5="." then NumPrb5=0;
if NumPrb6="." then NumPrb6=0;

run;
*********************************************************************
*  Finding number of prbs by hour is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*******    END Part 7   *********************************************
*********************************************************************;


*********************************************************************
*********************************   Start New Method    *************
****     From start of last E1 to end of EPG record
*********************************************************************;
data Ebert; set Ebert; TmLstE1EndRcrd=".";
*********************************************************************
*  Finding from first E1 to end of EPG record is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*********************************************************************
*********************************   Start New Method    *************
***** Time from Last E2 to end of EPG record 
*********************************************************************;
data three; set onlyE2;
data three; set three; proc sort; by inverter1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do; in0=insectno; marker1=0; end; else marker1=1;
data three; set three; if marker1=0 and waveform="E2" then output;
data three; set three; proc sort; by line;
data three; set three; TmLstE2EndRcrd=dur;
data three; set three; drop waveform dur line sumstart sumend instance inverter1 in0 marker1;
proc sort; by insectno;
data Ebert; set Ebert three; merge Ebert three; by insectno;
*********************************************************************
*  Finding time from the beginning of E2 to the end of the EPG record is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
***** Duration of longest E2
*********************************************************************;
Data three; set OnlyE2;
	if compress(upcase(waveform))='E2' then output;
data three; set three; proc means noprint;
	var dur; by insectno; output out=outsas max=maxE2;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nolist nodetails; delete three outsas;
*********************************************************************
*  Finding Duration of longest E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****** Find duration of NP following first sustained E2  ************
*********************************************************************;
Data three; set onlysuse2;
	retain in0 marker1;
	if in0 ne insectno then do; in0=insectno; marker1=0; end;
	if compress(upcase(waveform))='E2' and dur>600 then marker1=1;
data three; set three; if marker1=1 then output;
data three; set three; drop in0 marker1;
Data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do; in0=insectno; marker1=0; end;
	if compress(upcase(waveform))='NP' or compress(upcase(waveform))="Z" then marker1=1;
Data three; set three; if marker1=1 then output;
Data three; set three; drop in0 marker1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do; in0=insectno; marker1=0; end;
	if compress(upcase(waveform))ne'NP' and compress(upcase(waveform)) ne 'Z' then marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; DurNpFllwFrstSusE2=dur;
data three; set three; drop dur waveform line sumstart sumend instance inverter1 in0 marker1;
data Ebert; set Ebert three; merge Ebert three; by insectno;
proc datasets nolist nodetails; delete three;
*********************************************************************
*  Finding Find duration of NP following first sustained E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method         ********
***** Duration of NP just after sus E2 given NP last event   ********
*********************************************************************;
Data three; set onlysuse2;
proc sort; by inverter1;
data three; set three; *delete all insects that do not have an ending NP waveform;
	retain in0 marker1;
	if in0 ne insectno then do; in0=insectno; marker1=0; end;
	if waveform = "NP" or waveform = "Z" then marker1=1;
data three; set three; proc sort; by insectno line;
data three; set three; drop in0;
data three; set three;
	retain in0 marker2;
	if in0 ne insectno then do; in0=insectno; marker2=0; if marker1=1 then marker2=1; end;
data three; set three; if marker2=1 then output;
data three; set three; drop in0 marker1 marker2;
data three; set three; proc sort; by insectno inverter1;

Data three; set three;
	retain in0 RecDur;
	if in0 ne insectno then do; in0=insectno; RecDur=sumend; end;
data three; set three;
proc sort; by line;
data three; set three; drop in0;
Data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do; in0=insectno; marker1=0; end;
	if compress(upcase(waveform))='E2' and dur>600 then marker1=1;
data three; set three; if marker1=1 then output;
data three; set three; drop in0 marker1;
Data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do; in0=insectno; marker1=0; end;
	if compress(upcase(waveform))='NP' or compress(upcase(waveform))="Z" then marker1=1;
Data three; set three; if marker1=1 then output;
Data three; set three; drop in0 marker1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do; in0=insectno; marker1=0; end;
	if compress(upcase(waveform))ne'NP' and compress(upcase(waveform)) ne 'Z' then marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; 
	if sumend=RecDur then DurTrmNpFllwFrstSusE2=.; else DurTrmNpFllwFrstSusE2='.'; *NOTE: This variable is set to missing in all cases;
data three; set three; drop dur line sumstart RecDur sumend instance inverter1 in0 marker1;
data Ebert; set Ebert three; merge Ebert three; by insectno;
Proc datasets nolist nodetails; delete three;
*********************************************************************
*  Finding Duration of NP just after sus E2 given NP artificially terminated event is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****  Percent probing spent in C
*********************************************************************;
Data three; set one;
	if compress(upcase(waveform)) ne 'NP' and compress(upcase(waveform)) ne 'Z' then output;
data three; set three;
proc means noprint; var dur; by insectno; output out=outsas1 sum=SumPrb;
data three; set three;
	if compress(upcase(waveform))='C' then output;
data three; set three;
proc means noprint; var dur; by insectno; output out=outsas2 sum=sumC;
data outsas1; set outsas1 outsas2; merge outsas1 outsas2; by insectno;
data outsas1; set outsas1;
	PrcntPrbC=100*sumC/sumprb;
data outsas1; set outsas1; drop _TYPE_ _FREQ_ SumC SumPrb;
data Ebert; set Ebert outsas1; merge Ebert outsas1; by insectno;
data Ebert; set Ebert; if PrcntPrbC='.' then PrcntPrbC=0;
proc datasets nolist nodetails; delete outsas1 outsas2 three;
*********************************************************************
*  Finding Percent probing spent in C is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


***********************************************************************
*********************************   Start New Method    ***************
****  Percent probing spent in E1
*********************************************************************;
Data three; set OnlyE1;
	if compress(upcase(waveform)) ne 'NP' and compress(upcase(waveform)) ne 'Z' then output;
data three; set three;
proc means noprint; var dur; by insectno; output out=outsas1 sum=SumPrb;
data three; set three;
	if compress(upcase(waveform))='E1' then output;
data three; set three;
proc means noprint; var dur; by insectno; output out=outsas2 sum=sumC;
data outsas1; set outsas1 outsas2; merge outsas1 outsas2; by insectno;
data outsas1; set outsas1;
	PrcntPrbE1=100*sumC/sumprb;
data outsas1; set outsas1; drop _TYPE_ _FREQ_ SumC SumPrb;
data Ebert; set Ebert outsas1; merge Ebert outsas1; by insectno;
data Ebert; set Ebert; if PrcntPrbE1='.' then PrcntPrbE1=0;
proc datasets nolist nodetails; delete outsas1 outsas2 three;
*********************************************************************
*  Finding Percent probing spent in E1 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
*********************************   Start New Method    *************
****  Percent probing spent in E2
*********************************************************************;
Data three; set one;
	if compress(upcase(waveform)) ne 'NP' and compress(upcase(waveform)) ne 'Z' then output;
data three; set three;
proc means noprint; var dur; by insectno; output out=outsas1 sum=SumPrb;
data three; set three;
	if compress(upcase(waveform))='E2' then output;
data three; set three;
proc means noprint; var dur; by insectno; output out=outsas2 sum=sumC;
data outsas1; set outsas1 outsas2; merge outsas1 outsas2; by insectno;
data outsas1; set outsas1;
	PrcntPrbE2=100*sumC/sumprb;
data outsas1; set outsas1; drop _TYPE_ _FREQ_ SumC SumPrb;
data Ebert; set Ebert outsas1; merge Ebert outsas1; by insectno;
proc datasets nolist nodetails; delete outsas1 outsas2 three;
data Ebert; set Ebert; if PrcntPrbE2='.' then PrcntPrbE2=0;
*********************************************************************
*  Finding Percent probing spent in E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
****     Start New Method                                 ***********
****  Percent probing spent in G                          ***********
*********************************************************************;
Data three; set one;
	if compress(upcase(waveform)) ne 'NP' and compress(upcase(waveform)) ne 'Z' then output;
data three; set three;
proc means noprint; var dur; by insectno; output out=outsas1 sum=SumPrb;
data three; set three;
	if compress(upcase(waveform))='G' then output;
data three; set three;
proc means noprint; var dur; by insectno; output out=outsas2 sum=sumC;
data outsas1; set outsas1 outsas2; merge outsas1 outsas2; by insectno;
data outsas1; set outsas1;
	PrcntPrbG=100*sumC/sumprb;
data outsas1; set outsas1; drop _TYPE_ _FREQ_ SumC SumPrb;
data Ebert; set Ebert outsas1; merge Ebert outsas1; by insectno;
proc datasets nolist nodetails; delete outsas1 outsas2 three;
data Ebert; set Ebert; if PrcntPrbG='.' then PrcntPrbG=0;
*******************************************************************
*  Finding Percent probing spent in G is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXX*
*******************************************************************


*******************************************************************
****               Start New Method                     ***********
****       Percent E2 spent in Sustained E2             ***********
*******************************************************************;
Data three; set OnlyE2;
	if compress(upcase(waveform)) = 'E2' then marker1=1; else marker1=0;
data three; set three;
proc means noprint; var marker1; by insectno; output out=outsas1 sum=SumPrb;
data three; set three;
	if compress(upcase(waveform))='E2' and dur>600 then output;
data three; set three;
proc means noprint; var marker1; by insectno; output out=outsas2 sum=sumC;
data outsas1; set outsas1 outsas2; merge outsas1 outsas2; by insectno;
data outsas1; set outsas1;
	PrcntE2SusE2=100*sumC/sumprb;
data outsas1; set outsas1; drop _TYPE_ _FREQ_ SumC SumPrb;
data outsas1; set outsas1; if PrcntE2SusE2='.' then PrcntE2SusE2=0;
data Ebert; set Ebert outsas1; merge Ebert outsas1; by insectno;
proc datasets nolist nodetails; delete outsas1 outsas2 three;
Run;
*********************************************************************
*  Finding Percent E2 spent in Sustained E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


**********************************************************************
*****               THE END. START STATISTICAL ANALYSES     **********
**********************************************************************
**********************************************************************
*****             ******    ***********   *****       ****************
*****             ******     **********   *****   ***    *************
*****   ****************      *********   *****   ******   ***********
*****   ****************   *   ********   *****   ********   *********
*****   ****************   **   *******   *****   *********   ********
*****             ******   ***   ******   *****   **********  ********
*****             ******   ****   *****   *****   *********   ********
*****   ****************   *****   ****   *****   ********   *********
*****   ****************   ******   ***   *****   *******   **********
*****   ****************   *******   **   *****   *****   ************
*****             ******   ********   *   *****   ***   **************
*****             ******   *********      *****       ****************
**********************************************************************
**********************************************************************;

**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
*******                                                      *********
*******                      Clean-up                        *********
*******                                                      *********
*******                     Treatments                       *********
*******                                                      *********
*******               Zeros and Missing data                 *********
*******                                                      *********
*******                                                      *********
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************;

Data Ebert; Set Ebert; Drop waveform;
Data Ebert; Set Ebert; trt=substr(insectno,1,1);*recover treatment designations*;
Data Ebert; Set Ebert;
If NmbrShrtC="." then NmbrShrtC="0";
If NumPrb1="." then NumPrb1=0;
if TtlDurNp1="." then TtlDurNp1=0;

If maxdur<3600 then do; *if maxdur<3600 then the recording duration was less than 1 hour;
		If NumPrb2=0 then NumPrb2=".";
		if TtlDurNp2=0 then TtlDurNp2=".";
End;

If maxdur<7200 then do;
		If NumPrb3=0 then NumPrb3=".";
		if TtlDurNp3=0 then TtlDurNp3=".";
End;

If maxdur<10800 then do;
		If NumPrb4=0 then NumPrb4=".";
		if TtlDurNp4=0 then TtlDurNp4=".";

End;

If maxdur<14400 then do;
		If NumPrb5=0 then NumPrb5=".";
		if TtlDurNp5=0 then TtlDurNp5=".";

End;

If maxdur<18000 then do;
		If NumPrb6=0 then NumPrb6=".";
		if TtlDurNp6=0 then TtlDurNp6=".";

End;


If TmStrtEPGFrstE="." then TmStrtEPGFrstE=maxdur;
If TmFrmFrstPrbFrstE="." then TmFrmFrstPrbFrstE=maxdur;
if NumE2="." then NumE2=0;
if NumLngE2="." then NumLngE2=0;
if TmFrstSusE2="." then TmFrstSusE2=maxdur;
if TmFrstSusE2FrstPrb="." then TmFrstSusE2FrstPrb=maxdur;
if TmFrstE2StrtEPG="." then TmFrstE2StrtEPG=maxdur;
if TmFrstE2FrmFrstPrb="." then TmFrstE2FrmFrstPrb=maxdur-TmFrstPrbFrmStrt;

Data ebert; Set Ebert; drop maxdur;


*********************************************************************
***        MORE VARIABLES ASSOCIATED WITH D AND G              ******
***          BASED ON THE GENERIC VERSION OF EBERT             ******
***          EDITED TO REMOVE OVERLAP IN G                     ******
***                                                            ******
***         Sustained D does not make sense at 10 minutes.     ******
***         Some other value is needed, but I have not worked  ******
***            out what that should be.                        ******
***                                                            ******
*********************************************************************


Data Ebert; Set Ebert; Transform=1;  *If Transform=1, then the data will be transformed prior to analysis, set transform=0 to change this;
Run;

Data one; set one;
      line=_n_;
*     Calculate time to start and time to end of each behavior.;
      retain in0 SumStart SumEnd dur0;
      if insectno ne in0 then do;
       SumStart=0.0; SumEnd=0.0; dur0=0.0;
       in0=insectno;
      end;
      SumEnd=SumEnd+dur;
      SumStart=SumStart+Dur0;
      dur0=dur;
	proc sort;by insectno waveform line;
data one; set one;
waveform=compress(upcase(waveform));
if dur ne "." then output;

data one; set one; proc sort; by insectno waveform;
data one;set one; by insectno waveform;
	retain instance;
	if first.waveform then instance=0;
	instance=instance+1;
Data one; set one; proc sort; by line;
Data one; set one;
	inverter1=50000-line;
Data one; set one; drop in0 dur0;

Data one; set one;
retain in0 holder1;
if in0 ne insectno then do; in0=insectno; holder1=0; end;
holder1 = holder1+dur;
data one; set one; drop in0;
data one; set one; proc sort; by inverter1;
data one; set one;
retain in0 maxdur;
if in0 ne insectno then do; in0=insectno; maxdur=holder1; end;
data one; set one; drop in0 holder1;
data one; set one; proc sort; by line;
Run;

*****************************************************************************
*****************************************************************************
*****************************************************************************
*****************************   NEW VARIABLES   *****************************
*****************************   Not in Sarria   *****************************
*****************************************************************************
*****************************************************************************
*****************************************************************************;
Data one; Set one;
DurSusD=100;
DurSusG=600;
Run;  

******************************************************************
******************   Start New Method    *************************
*******         define the dataset OnlyD                      ***
******************************************************************;
Data three; set one; Proc sort; by insectno line;
Data three; set three;
	retain in0 marker1;
	if insectno ne in0 then do;
		in0=insectno; marker1=0; 
	end;
    if compress(upcase(waveform))='D' then Marker1=1;
Data three; set three;
Proc sort; by insectno Inverter1;
Data three; set three; drop in0;
Data three; Set three;
	retain in0 marker2;
	if insectno ne in0 then do;
	  marker2=0; in0=insectno; 
	end;
	if marker1=1 then marker2=1;
	if marker1=0 and marker2=1 then marker2=1;
Data three; set three;
if marker2=1 then output;
Data three; set three; Proc sort; by insectno line;
data three; set three; drop marker1 marker2 in0;
Data OnlyD; Set three;
run;
******************************************************************
*  Finished creating dataset OnlyD
* XXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/

******************************************************************
**********************   Start New Method    *********************
*******            define the dataset OnlySusD               *****
*******       The correct duration that defines a sustained  *****
*******         D is highly uncertain. It is uncertain what  *****
*******         it means to have a sustained D.              *****
*******         For no good reason, I entered 100 seconds.   *****
******************************************************************;
Data three; set one; Proc sort; by insectno line;
Data three; set three;
	retain in0 marker1;
	if insectno ne in0 then do;
		in0=insectno; marker1=0; 
	end;
    if compress(upcase(waveform))='D' and dur>DurSusD then Marker1=1;
Data three; set three;
Proc sort; by insectno Inverter1;
Data three; set three; drop in0;
Data three; Set three;
	retain in0 marker2;
	if insectno ne in0 then do;
	  marker2=0; in0=insectno; 
	end;
	if marker1=1 then marker2=1;
	if marker1=0 and marker2=1 then marker2=1;
Data three; set three;
if marker2=1 then output;
Data three; set three; Proc sort; by insectno line;
data three; set three; drop marker1 marker2 in0;
Data OnlySusD; Set three;
run;
******************************************************************
*  Finished creating dataset OnlySusD
* XXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXX*/;

proc datasets nodetails nolist; delete three;

******************************************************************
**********     THE END. START Computing Variables      ***********
******************************************************************
******************************************************************
*****             ******    ***********   *****       ************
*****             ******     **********   *****   ***    *********
*****   ****************      *********   *****   ******   *******
*****   ****************   *   ********   *****   ********   *****
*****   ****************   **   *******   *****   *********   ****
*****             ******   ***   ******   *****   **********   ***
*****             ******   ****   *****   *****   *********   ****
*****   ****************   *****   ****   *****   ********   *****
*****   ****************   ******   ***   *****   *******   ******
*****   ****************   *******   **   *****   *****   ********
*****             ******   ********   *   *****   ***   **********
*****             ******   *********      *****       ************
******************************************************************
******************************************************************;

******************************************************************
******************************************************************
******************************************************************
******************************************************************
******************************************************************
******************************************************************
************                                         *************
************              Variables                  *************
************                                         *************
************                                         *************
************                                         *************
************                                         *************
******************************************************************
******************************************************************
******************************************************************
******************************************************************
******************************************************************
******************************************************************
******************************************************************
******************************************************************;

******************************************************************
/******************   Start New Method    ************************
*  Number of probes to first D.
*****************************************************************/;

Data three; set OnlyD;
Data three; set three; Proc sort; by insectno line;
Data three; set three;
	retain in0 marker1 marker2;
	w1=Compress(upcase(waveform));
	if insectno ne in0 then do;
	 marker1=0; Marker2=0;
	 in0=insectno;
	end;
	If w1='C' then marker1=1;
	If w1='Z' or w1='NP' then marker1=0;
	if w1='D' then marker2=1;
Data three; set three; if marker2=0 then output;
data three; set three; drop marker2 in0;
Data three; set three;
	retain in0 marker3 marker4;
	if insectno ne in0 then do;
	marker3=0; marker4=0; in0=insectno;
	end;
	if marker1=1 and marker3=0 then marker4=marker4+1;
	marker3=marker1;
Data three; set three; drop in0 marker1 marker3;
Data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain marker1 in0;
	if insectno ne in0 then do;
	marker1=0; in0=insectno;
	end;
	else marker1=1;
Data three; set three; if marker1=0 then output;
Data three; set three; CtoFrstD=marker4;
Data three; Set three; drop waveform dur line sumstart sumend instance inverter1 w1 in0 marker4 marker1;
data Ebert; set Ebert three; merge Ebert three; by insectno;
run;
******************************************************************
*  Finding number of probes to first D is finished.
* XXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************;

******************************************************************
/********************   Start New Method    **********************
The number of D
******************************************************************/;
data three; set OnlyD; if compress(upcase(waveform))='D' then output;
proc sort; by insectno inverter1;
data three; set three; 
retain in0 marker1;
if in0 eq insectno then do;
	in0=insectno; marker1=0;
end;
else marker1=1;
data three; set three;
proc means noprint; var instance; by insectno; output out=outsas max=NumD;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
data Ebert; set Ebert; if NumD='.' then NumD="0";

******************************************************************
*  Finding the number of D is finished.
* XXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************;


******************************************************************
/**********************   Start New Method    ********************
*	Total duration of D
*****************************************************************/;
Data three; set OnlyD; if compress(upcase(waveform))='D' then output;
Data three; set three;
	retain in0 TtlDurD;
	if insectno ne in0 then do;
		in0=insectno; TtlDurD=0;
	end;
	TtlDurD=TtlDurD+dur;
Data three; set three; proc sort; by insectno inverter1;
Data three; set three; drop in0;
Data three; set three;
	retain in0 marker1;
	if insectno ne in0 then do;
		in0=insectno; marker1=1;
	end;
	else marker1=0;
Data three; set three; if marker1=1 then output;
Data three; Set three; drop waveform dur line sumstart sumend instance inverter1 in0 marker1;
data Ebert; set Ebert three; merge Ebert three; by insectno;
******************************************************************
*  Finding Total duration of D is finished.
* XXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************
******************************************************************

******************************************************************
*****************   Start New Method    **************************
**	Duration of nonprobe period before the first D
******************************************************************;
Data three; set OnlyD; 
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do; in0=insectno; marker1=0; end;
	if compress(upcase(waveform)) eq 'D' then marker1=1;
Data three; set three; if marker1=0 then output;
data three; set three; drop in0 marker1;
Data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do; in0=insectno; marker1=0; end;
	if compress(upcase(waveform)) eq 'Z' or compress(upcase(waveform)) eq 'NP' then marker1=1;
Data three; set three; if marker1=1 then output;
Data three; set three; if compress(upcase(waveform)) eq 'Z' or compress(upcase(waveform)) eq 'NP' then output;
Data three; set three; drop in0 marker1;
data three; set three; proc sort; by insectno line;
data three; set three; 
	retain DurNnprbBfrFrstD in0;
	if in0 ne insectno then do; in0=insectno; DurNnprbBfrFrstD=0; end;
	DurNnprbBfrFrstD=DurNnprbBfrFrstD+dur;
Data three; set three; drop in0;
data three; set three; proc sort; by insectno inverter1;
Data three; set three;
	retain in0 marker1;
	if insectno ne in0 then do;
		in0=insectno; marker1=1;
	end;
	else marker1=0;
Data three; set three; if marker1=1 then output;
Data three; set three; drop waveform dur line sumstart sumend instance inverter1 in0 marker1;
data Ebert; set Ebert three; merge Ebert three; by insectno;
******************************************************************
*  Finding Duration of nonprobe period before the first D is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************

******************************************************************
/*********************************   Start New Method    ******************************
*	Mean duration of D
******************************************************************;
Data three; set OnlyD;
data three; set three; if compress(upcase(waveform))='D' then output;
Data three; set three; proc means noprint; by insectno; var dur; output out=outsas mean=meanD;
data three; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert three; merge Ebert three; by insectno;
Proc datasets nolist nodetails; delete outsas three;
******************************************************************
*  Finding Mean duration of D is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************

******************************************************************
******************************************************************
/*********************************   Start New Method    ******************************
**	Average number of D per probe
******************************************************************;
Data three; set OnlyD; 								*mark each probe.;
	retain in0 marker1;
	w1=compress(upcase(waveform));
	if insectno ne in0 then do;
		in0=insectno; marker1=0;
	end;
	if w1='C' then marker1=1;
	if w1='Z' or w1='NP' then marker1=0;
Data three; set three; drop in0;
Data three; set three; 									*counting the number of probes;
	retain in0 holder1 marker2;
	if insectno ne in0 then do;
		in0=insectno; marker2=0; holder1=0;
	end;
	if holder1 ne marker1  and compress(upcase(waveform))='C' then do;
		marker2=marker2+1;
	end;
	holder1=marker1;
Data three; set three; drop in0 holder1;
Data three; set three;								*counting D in each probe;
	retain in0 holder1 marker3 marker4;
	if insectno ne in0 then do;
		in0=insectno; marker3=0; holder1=0; marker4=0;
	end;
	if marker1=1 and holder1=0 then marker3=1;
	if marker1=0 and holder1=1 then marker3=0;
	if marker3=1 and compress(upcase(waveform))='D' then marker4=marker4+1;
	if marker3=0 then marker4=0;
	holder1=marker1;
Data three; set three; drop in0 holder1 marker3;
Data three; set three; proc sort; by insectno inverter1;  *Isolate the last entry in each probe;
Data three; set three;
	retain in0 holder1 marker5;
	if insectno ne in0 then do;
		in0=insectno; holder1=0; marker5=0;
	end;
	if holder1=0 and marker1=1 then marker5=1;
	else marker5=0;
	holder1=marker1;
data three; set three; drop in0 holder1;
data three; set three; if marker5=1 then output;
data three; set three; proc sort; by insectno line;
data three; set three; proc means noprint; var marker4; by insectno; output out=outsas mean=meanNDPrb;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete outsas three;
******************************************************************
*  Finding Average number of D per probe is finished.
* XXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************
******************************************************************
/***************   Start New Method    ***************************
**	Time from first probe to 1st D
******************************************************************;
Data three; set onlyD;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='D' then  marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; drop in0 marker1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='C' then marker1=1;
Data three; set three; if marker1=1 then output;
Data three; set three; drop in0 marker1;
data three; set three;
	retain in0 TmFrmFrstPrbFrstD;
	if in0 ne insectno then do;
		in0=insectno; TmFrmFrstPrbFrstD=0;
	end;
	TmFrmFrstPrbFrstD = TmFrmFrstPrbFrstD + dur;
data three; set three; drop in0;
data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	else marker1=1;
data three; set three; proc sort; by line;
data three; set three; if marker1=0 then output;
data three; set three; drop waveform dur line sumstart sumend instance inverter1 in0 marker1;
data Ebert; set Ebert three; merge Ebert three; by insectno;
******************************************************************
*  Finding Time from 1st probe to 1st D is finished.
* XXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************

******************************************************************
/*************   Start New Method    *****************************
**	Time from start of probe with first D to 1st D
******************************************************************;
Data three; set onlyD;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='D' then  marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; drop in0 marker1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='C' then marker1=1;
Data three; set three; if marker1=1 then output;
Data three; set three; drop in0 marker1;
data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='Z' or compress(upcase(waveform))='NP' then marker1=1;
data three; set three; proc sort; by line;
data three; set three; if marker1=0 then output;
Data three; set three; drop in0 marker1;
data three; set three;
	retain in0 TmBegPrbFrstD;
	if in0 ne insectno then do;
		in0=insectno; TmBegPrbFrstD=0;
	end;
	TmBegPrbFrstD=TmBegPrbFrstD+dur;
data three; set three; drop in0;
data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	else marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; drop waveform dur line sumstart sumend instance inverter1 in0 marker1;
data Ebert; set Ebert three; merge Ebert three; by insectno;

******************************************************************
*  Finding Time from start of probe with first D to 1st D is finished.
* XXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************

******************************************************************
/***************   Start New Method    ***************************
**	Number of Probes after first D
******************************************************************;
Data three; set OnlyD;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='D' then marker1=1;
data three; set three; if marker1=1 then output;
data three; set three; drop in0 marker1;

data three; set three;
	retain in0 marker2 delay1;
	if in0 ne insectno then do;
		in0=insectno; marker2=0; delay1=1;
	end;
	if delay1=0 then do;
		if compress(upcase(waveform))='Z' then marker2=marker2+1;
		if compress(upcase(waveform))='NP' then marker2=marker2+1;
		 
	end;
	delay1=0;
data three; set three; if compress(upcase(waveform))='C' then output;
data three; set three; proc means noprint;
	by insectno; var marker2; output out=outsas max=NumPrbsAftrFrstD;
data outsas; set outsas; if NumPrbsAftrFrstD='.' then NumPrbsAftrFrstD=0;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
Data Ebert; set Ebert; if NumPrbsAftrFrstD='.' then NumPrbsAftrFrstD=0;
proc datasets nodetails nolist; delete three outsas;
run;
******************************************************************
*  Finding Number of probes after first D is finished.
* XXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************

******************************************************************
/******************   Start New Method    ************************
**	Number of Probes<3min after first D
******************************************************************;
Data three; set OnlyD;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='D' then marker1=1;
data three; set three; if marker1=1 then output;
data three; set three; drop in0 marker1;

Data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='NP' or compress(upcase(waveform))="Z" then marker1=1;
data three; set three; if marker1=1 then output;
data three; set three; drop in0 marker1;
data three; set three;
	retain in0 marker2 delay1;
	if in0 ne insectno then do;
		in0=insectno; marker2=1; delay1=1;
	end;
	if delay1=0 then do;
		if compress(upcase(waveform))='Z' then marker2=marker2+1;
		if compress(upcase(waveform))='NP' then marker2=marker2+1;
		 
	end;
	delay1=0;
Data three; set three; if compress(upcase(waveform)) ne "Z" and compress(upcase(waveform)) ne "NP" then waveform="PRB";
data three; set three; 
  proc sort; by insectno marker2 waveform;
  proc means noprint; by insectno marker2 waveform; var dur; output out=outsas2 sum=sdur;
data outsas2; set outsas2; if waveform="PRB" and sdur<180 then output;
data outsas2; set outsas2;
	retain in0 marker1;
	if in0 ne insectno then do;	in0=insectno; marker1=0; end;
	marker1=marker1+1;
Data outsas2; set outsas2;
  proc means noprint; by insectno; var marker1; output out=outsas4 max=NmbrShrtPrbAftrFrstD;

data outsas4; set outsas4; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas4; merge Ebert outsas4; by insectno;
Data Ebert; set Ebert; if NmbrShrtPrbAftrFrstD='.' then NmbrShrtPrbAftrFrstD=0;
proc datasets nolist nodetails; delete three outsas4 outsas2;
run;
******************************************************************
*  Finding Number of probes <3min after first D is finished.
* XXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************

******************************************************************
/******************   Start New Method    ************************
***   Find Number of sustained D;
******************************************************************;
Data three; set OnlyD;
if compress(upcase(waveform))='D' and dur>dursusD then marker1=1; else marker1=0;
data three; set three; proc means noprint;
	by insectno; var marker1; output out=outsas sum=NumLngD;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
******************************************************************
*  Finding Number of sustained D is finished.
* XXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************


******************************************************************
/***********************   Start New Method    *******************
********     Time to first sustained D from first probe   *******
******************************************************************;
Data three; set OnlySusD; 
retain in0 marker1;
if in0 ne insectno then do; in0=insectno; marker1=0; end;
if compress(upcase(waveform))="D" and dur>DurSusD then marker1=1;
data three; set three; if marker1=0 then output;
data three; set three;
proc means noprint; var sumend; by insectno; output out=outsas max=TmSusD;
data four; set one;
proc means noprint; var sumend; by insectno; output out=outsas1 max=runtime;
data outsas; set outsas outsas1; merge outsas outsas1; by insectno;
TmFrstSusD=TmSusD;
data outsas; set outsas; drop runtime tmsusD _TYPE_ _FREQ_;
data three; set one; retain marker1;
data three; set three;
retain in0 marker1;
if in0 ne insectno then do; in0=insectno; marker1=0; end;
if compress(upcase(waveform))="C" then marker1=1;
Data three; set three; If marker1=0 then output;
data three; set three; drop in0 marker1;
Data three; set three;
proc means noprint; var dur; by insectno; output out=outsas3 sum=Sumdur;
Data outsas3; set outsas3; drop _TYPE_ _FREQ_;
data outsas; set outsas outsas3; merge outsas outsas3; by insectno;
data outsas; set outsas; TmFrstSusDFrstPrb=TmfrstSusD-Sumdur;
data outsas; set outsas; if TmFrstSusDFrstPrb<=0 then TmFrstSusDFrstPrb=".";
data outsas; set outsas; drop TmfrstSusD Sumdur;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four outsas outsas1 outsas3 three;
******************************************************************
*  Finding first sustained E2 from first probe is finished.
* XXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************


******************************************************************
/******************   Start New Method    ************************
*****               Duration of longest D
******************************************************************;
Data three; set OnlyD;
	if compress(upcase(waveform))='D' then output;
data three; set three; proc means noprint;
	var dur; by insectno; output out=outsas max=maxD;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nolist nodetails; delete three outsas;
******************************************************************
*  Finding Duration of longest E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************



******************************************************************
/***************   Start New Method    ***************************
****  Percent probing spent in D
******************************************************************;
Data three; set OnlyD;
	if compress(upcase(waveform)) ne 'NP' and compress(upcase(waveform)) ne 'Z' then output;
data three; set three;
proc means noprint; var dur; by insectno; output out=outsas1 sum=SumPrb;
data three; set three;
	if compress(upcase(waveform))='D' then output;
data three; set three;
proc means noprint; var dur; by insectno; output out=outsas2 sum=sumD;
data outsas1; set outsas1 outsas2; merge outsas1 outsas2; by insectno;
data outsas1; set outsas1;
	PrcntPrbD=100*sumD/sumprb;
data outsas1; set outsas1; drop _TYPE_ _FREQ_ SumPrb;
data Ebert; set Ebert outsas1; merge Ebert outsas1; by insectno;
data Ebert; set Ebert; if PrcntPrbD='.' then PrcntPrbD=0;
proc datasets nolist nodetails; delete outsas1 outsas2 three;
******************************************************************
*  Finding Percent probing spent in D is finished.
* XXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************;

**********************************************************************************
**********************************************************************************
**********************************************************************************

******************************************************************
**********************   Start New Method    *********************
*******            define the dataset OnlySusG       *      *****
******************************************************************;
Data three; set one; Proc sort; by insectno line;
Data three; set three;
	retain in0 marker1;
	if insectno ne in0 then do;
		in0=insectno; marker1=0; 
	end;
    if compress(upcase(waveform))='G' and dur>DurSusG then Marker1=1;
Data three; set three;
Proc sort; by insectno Inverter1;
Data three; set three; drop in0;
Data three; Set three;
	retain in0 marker2;
	if insectno ne in0 then do;
	  marker2=0; in0=insectno; 
	end;
	if marker1=1 then marker2=1;
	if marker1=0 and marker2=1 then marker2=1;
Data three; set three;
if marker2=1 then output;
Data three; set three; Proc sort; by insectno line;
data three; set three; drop marker1 marker2 in0 dur0;
Data OnlySusG; Set three;
run;
******************************************************************
*  Finished creating dataset OnlySusG
* XXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXX*/;

proc datasets nodetails nolist; delete three;

******************************************************************
**********                                             ***********
******************************************************************
******************************************************************
*****             ******    ***********   *****       ************
*****             ******     **********   *****   ***    *********
*****   ****************      *********   *****   ******   *******
*****   ****************   *   ********   *****   ********   *****
*****   ****************   **   *******   *****   *********   ****
*****             ******   ***   ******   *****   **********   ***
*****             ******   ****   *****   *****   *********   ****
*****   ****************   *****   ****   *****   ********   *****
*****   ****************   ******   ***   *****   *******   ******
*****   ****************   *******   **   *****   *****   ********
*****             ******   ********   *   *****   ***   **********
*****             ******   *********      *****       ************
******************************************************************
******************************************************************;

******************************************************************
******************************************************************
******************************************************************
******************************************************************
******************************************************************
******************************************************************
************                                         *************
************              Variables                  *************
************                                         *************
************                                         *************
************                                         *************
************                                         *************
******************************************************************
******************************************************************
******************************************************************
******************************************************************
******************************************************************
******************************************************************
******************************************************************
******************************************************************;



******************************************************************
/******************   Start New Method    ************************
*  Number of probes to first G.
*****************************************************************/;

Data three; set OnlyG;
Data three; set three; Proc sort; by insectno line;
Data three; set three;
	retain in0 marker1 marker2;
	w1=Compress(upcase(waveform));
	if insectno ne in0 then do;
	 marker1=0; Marker2=0;
	 in0=insectno;
	end;
	If w1='C' then marker1=1;
	If w1='Z' or w1='NP' then marker1=0;
	if w1='G' then marker2=1;
Data three; set three; if marker2=0 then output;
data three; set three; drop marker2 in0;
Data three; set three;
	retain in0 marker3 marker4;
	if insectno ne in0 then do;
	marker3=0; marker4=0; in0=insectno;
	end;
	if marker1=1 and marker3=0 then marker4=marker4+1;
	marker3=marker1;
Data three; set three; drop in0 marker1 marker3;
Data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain marker1 in0;
	if insectno ne in0 then do;
	marker1=0; in0=insectno;
	end;
	else marker1=1;
Data three; set three; if marker1=0 then output;
Data three; set three; CtoFrstG=marker4;
Data three; Set three; drop waveform dur line sumstart sumend instance inverter1 w1 in0 marker4 marker1 dur0;
data Ebert; set Ebert three; merge Ebert three; by insectno;
run;
******************************************************************
*  Finding number of probes to first G is finished.
* XXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************;


******************************************************************
*****************   Start New Method    **************************
**	Duration of nonprobe period before the first G
******************************************************************;
Data three; set OnlyG; 
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do; in0=insectno; marker1=0; end;
	if compress(upcase(waveform)) eq 'G' then marker1=1;
Data three; set three; if marker1=0 then output;
data three; set three; drop in0 marker1;
Data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do; in0=insectno; marker1=0; end;
	if compress(upcase(waveform)) eq 'Z' or compress(upcase(waveform)) eq 'NP' then marker1=1;
Data three; set three; if marker1=1 then output;
Data three; set three; if compress(upcase(waveform)) eq 'Z' or compress(upcase(waveform)) eq 'NP' then output;
Data three; set three; drop in0 marker1;
data three; set three; proc sort; by insectno line;
data three; set three; 
	retain DurNnprbBfrFrstG in0;
	if in0 ne insectno then do; in0=insectno; DurNnprbBfrFrstG=0; end;
	DurNnprbBfrFrstG=DurNnprbBfrFrstG+dur;
Data three; set three; drop in0;
data three; set three; proc sort; by insectno inverter1;
Data three; set three;
	retain in0 marker1;
	if insectno ne in0 then do;
		in0=insectno; marker1=1;
	end;
	else marker1=0;
Data three; set three; if marker1=1 then output;
Data three; set three; drop waveform dur line sumstart sumend instance inverter1 in0 marker1 dur0;
data Ebert; set Ebert three; merge Ebert three; by insectno;
******************************************************************
*  Finding Duration of nonprobe period before the first G is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************


******************************************************************
******************************************************************
/*********************************   Start New Method    ******************************
**	Average number of G per probe
******************************************************************;
Data three; set OnlyG; 								*mark each probe.;
	retain in0 marker1;
	w1=compress(upcase(waveform));
	if insectno ne in0 then do;
		in0=insectno; marker1=0;
	end;
	if w1='C' then marker1=1;
	if w1='Z' or w1='NP' then marker1=0;
Data three; set three; drop in0;
Data three; set three; 									*counting the number of probes;
	retain in0 holder1 marker2;
	if insectno ne in0 then do;
		in0=insectno; marker2=0; holder1=0;
	end;
	if holder1 ne marker1  and compress(upcase(waveform))='C' then do;
		marker2=marker2+1;
	end;
	holder1=marker1;
Data three; set three; drop in0 holder1;
Data three; set three;								*counting G in each probe;
	retain in0 holder1 marker3 marker4;
	if insectno ne in0 then do;
		in0=insectno; marker3=0; holder1=0; marker4=0;
	end;
	if marker1=1 and holder1=0 then marker3=1;
	if marker1=0 and holder1=1 then marker3=0;
	if marker3=1 and compress(upcase(waveform))='G' then marker4=marker4+1;
	if marker3=0 then marker4=0;
	holder1=marker1;
Data three; set three; drop in0 holder1 marker3;
Data three; set three; proc sort; by insectno inverter1;  *Isolate the last entry in each probe;
Data three; set three;
	retain in0 holder1 marker5;
	if insectno ne in0 then do;
		in0=insectno; holder1=0; marker5=0;
	end;
	if holder1=0 and marker1=1 then marker5=1;
	else marker5=0;
	holder1=marker1;
data three; set three; drop in0 holder1;
data three; set three; if marker5=1 then output;
data three; set three; proc sort; by insectno line;
data three; set three; proc means noprint; var marker4; by insectno; output out=outsas mean=meanNGPrb;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete outsas three;
******************************************************************
*  Finding Average number of G per probe is finished.
* XXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************
******************************************************************
/***************   Start New Method    ***************************
**	Time from first probe to 1st G
******************************************************************;
Data three; set onlyG;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='G' then  marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; drop in0 marker1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='C' then marker1=1;
Data three; set three; if marker1=1 then output;
Data three; set three; drop in0 marker1;
data three; set three;
	retain in0 TmFrmFrstPrbFrstG;
	if in0 ne insectno then do;
		in0=insectno; TmFrmFrstPrbFrstG=0;
	end;
	TmFrmFrstPrbFrstG = TmFrmFrstPrbFrstG + dur;
data three; set three; drop in0;
data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	else marker1=1;
data three; set three; proc sort; by line;
data three; set three; if marker1=0 then output;
data three; set three; drop waveform dur line sumstart sumend instance inverter1 in0 marker1 dur0;
data Ebert; set Ebert three; merge Ebert three; by insectno;
******************************************************************
*  Finding Time from 1st probe to 1st G is finished.
* XXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************

******************************************************************
/*************   Start New Method    *****************************
**	Time from start of probe with first G to 1st G
******************************************************************;
Data three; set onlyG;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='G' then  marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; drop in0 marker1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='C' then marker1=1;
Data three; set three; if marker1=1 then output;
Data three; set three; drop in0 marker1;
data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='Z' or compress(upcase(waveform))='NP' then marker1=1;
data three; set three; proc sort; by line;
data three; set three; if marker1=0 then output;
Data three; set three; drop in0 marker1;
data three; set three;
	retain in0 TmBegPrbFrstG;
	if in0 ne insectno then do;
		in0=insectno; TmBegPrbFrstG=0;
	end;
	TmBegPrbFrstG=TmBegPrbFrstG+dur;
data three; set three; drop in0;
data three; set three; proc sort; by insectno inverter1;
data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	else marker1=1;
data three; set three; if marker1=0 then output;
data three; set three; drop waveform dur line sumstart sumend instance inverter1 in0 marker1 dur0;
data Ebert; set Ebert three; merge Ebert three; by insectno;

******************************************************************
*  Finding Time from start of probe with first G to 1st G is finished.
* XXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************

******************************************************************
/***************   Start New Method    ***************************
**	Number of Probes after first G
******************************************************************;
Data three; set OnlyG;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='G' then marker1=1;
data three; set three; if marker1=1 then output;
data three; set three; drop in0 marker1;

data three; set three;
	retain in0 marker2 delay1;
	if in0 ne insectno then do;
		in0=insectno; marker2=0; delay1=1;
	end;
	if delay1=0 then do;
		if compress(upcase(waveform))='Z' then marker2=marker2+1;
		if compress(upcase(waveform))='NP' then marker2=marker2+1;
		 
	end;
	delay1=0;
data three; set three; if compress(upcase(waveform))='C' then output;
data three; set three; proc means noprint;
	by insectno; var marker2; output out=outsas max=NumPrbsAftrFrstG;
data outsas; set outsas; if NumPrbsAftrFrstG='.' then NumPrbsAftrFrstG=0;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
Data Ebert; set Ebert; if NumPrbsAftrFrstG='.' then NumPrbsAftrFrstG=0;
proc datasets nodetails nolist; delete three outsas;
run;
******************************************************************
*  Finding Number of probes after first G is finished.
* XXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************

******************************************************************
/******************   Start New Method    ************************
**	Number of Probes<3min after first G
******************************************************************;
Data three; set OnlyG;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='G' then marker1=1;
data three; set three; if marker1=1 then output;
data three; set three; drop in0 marker1;

Data three; set three;
	retain in0 marker1;
	if in0 ne insectno then do;
		in0=insectno; marker1=0;
	end;
	if compress(upcase(waveform))='NP' or compress(upcase(waveform))="Z" then marker1=1;
data three; set three; if marker1=1 then output;
data three; set three; drop in0 marker1;
data three; set three;
	retain in0 marker2 delay1;
	if in0 ne insectno then do;
		in0=insectno; marker2=1; delay1=1;
	end;
	if delay1=0 then do;
		if compress(upcase(waveform))='Z' then marker2=marker2+1;
		if compress(upcase(waveform))='NP' then marker2=marker2+1;
		 
	end;
	delay1=0;
Data three; set three; if compress(upcase(waveform)) ne "Z" and compress(upcase(waveform)) ne "NP" then waveform="PRB";
data three; set three; 
  proc sort; by insectno marker2 waveform;
  proc means noprint; by insectno marker2 waveform; var dur; output out=outsas2 sum=sdur;
data outsas2; set outsas2; if waveform="PRB" and sdur<180 then output;
data outsas2; set outsas2;
	retain in0 marker1;
	if in0 ne insectno then do;	in0=insectno; marker1=0; end;
	marker1=marker1+1;
Data outsas2; set outsas2;
  proc means noprint; by insectno; var marker1; output out=outsas4 max=NmbrShrtPrbAftrFrstG;

data outsas4; set outsas4; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas4; merge Ebert outsas4; by insectno;
Data Ebert; set Ebert; if NmbrShrtPrbAftrFrstG='.' then NmbrShrtPrbAftrFrstG=0;
proc datasets nolist nodetails; delete three outsas4 outsas2;
run;
******************************************************************
*  Finding Number of probes <3min after first G is finished.
* XXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************

******************************************************************
/******************   Start New Method    ************************
***   Find Number of sustained G;
******************************************************************;
Data three; set OnlyG;
if compress(upcase(waveform))='G' and dur>600 then marker1=1; else marker1=0;
data three; set three; proc means noprint;
	by insectno; var marker1; output out=outsas sum=NumLngG;
data outsas; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
******************************************************************
*  Finding Number of sustained G is finished.
* XXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************


******************************************************************
/***********************   Start New Method    *******************
********     Time to first sustained G from first probe   *******
******************************************************************;
Data three; set OnlySusG; 
retain in0 marker1;
if in0 ne insectno then do; in0=insectno; marker1=0; end;
if compress(upcase(waveform))="G" and dur>DurSusG then marker1=1;
data three; set three; if marker1=0 then output;
data three; set three;
proc means noprint; var sumend; by insectno; output out=outsas max=TmSusG;
data four; set one;
proc means noprint; var sumend; by insectno; output out=outsas1 max=runtime;
data outsas; set outsas outsas1; merge outsas outsas1; by insectno;
TmFrstSusG=TmSusG;
data outsas; set outsas; drop runtime tmsusG _TYPE_ _FREQ_;
data three; set one; retain marker1;
data three; set three;
retain in0 marker1;
if in0 ne insectno then do; in0=insectno; marker1=0; end;
if compress(upcase(waveform))="C" then marker1=1;
Data three; set three; If marker1=0 then output;
data three; set three; drop in0 marker1;
Data three; set three;
proc means noprint; var dur; by insectno; output out=outsas3 sum=Sumdur;
Data outsas3; set outsas3; drop _TYPE_ _FREQ_;
data outsas; set outsas outsas3; merge outsas outsas3; by insectno;
data outsas; set outsas; TmFrstSusGFrstPrb=TmfrstSusG-Sumdur;
data outsas; set outsas; if TmFrstSusGFrstPrb<=0 then TmFrstSusGFrstPrb=".";
data outsas; set outsas; drop TmfrstSusG Sumdur;
data Ebert; set Ebert outsas; merge Ebert outsas; by insectno;
proc datasets nodetails nolist; delete four outsas outsas1 outsas3 three;
******************************************************************
*  Finding first sustained G from first probe is finished.
* XXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*
******************************************************************

*******************************************************************
*******************************************************************
*******************************************************************
****                                                           ****
**** It is possible that treatments could change the variance  ****
****     while having little or no influence on the means.     ****
**** ALSO note that the assumption for many models is          **** 
****     equality of variances: Homoscedasticity versus        ****
****     Heteroscedasticity. If the following variables have   ****
****     significant treatment effects then the data are       ****
****     heteroscedastic.                                      ****
****                                                           ****
**** Note: a non-significant outcome is not sufficient to      ****
****     demonstrate homoscedasticity.                         ****
****                                                           ****
**** I have added five variables to look at variance.          ****
****   sdC = standard deviation of the mean duration of C      ****
****             for each insect.                              ****
****   sdD = standard deviation of the mean duration of D      ****
****             for each insect.                              ****
****   sdG = standard deviation of the mean duration of G      ****
****             for each insect.                              ****
****   sdE1 = standard deviation of the mean duration of E1    ****
****             for each insect.                              ****
****   sdE2 = standard deviation of the mean duration of E2    ****
****             for each insect.                              ****
*******************************************************************
*******************************************************************
*******************************************************************

*********************************************************************
****                            Start New Method    *****************
****       Mean Deviation of C
*********************************************************************;
Data three; set one;
data three; set three; if compress(upcase(waveform))='C' then output;
Data three; set three; proc means noprint; by insectno; var dur; output out=outsas stddev=sdC;
data three; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert three; merge Ebert three; by insectno;
Proc datasets nolist nodetails; delete oned outsas three;
*********************************************************************
*  Finding Mean deviation of C is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
****                            Start New Method    *****************
****       Mean Deviation of D
*********************************************************************;
Data three; set OnlyD;
data three; set three; if compress(upcase(waveform))='D' then output;
Data three; set three; proc means noprint; by insectno; var dur; output out=outsas stddev=sdD;
data three; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert three; merge Ebert three; by insectno;
Proc datasets nolist nodetails; delete oned outsas three;
*********************************************************************
*  Finding Mean deviation of D is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
****                            Start New Method    *****************
****       Mean Deviation of G
*********************************************************************;
Data three; set OnlyG;
data three; set three; if compress(upcase(waveform))='G' then output;
Data three; set three; proc means noprint; by insectno; var dur; output out=outsas stddev=sdG;
data three; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert three; merge Ebert three; by insectno;
Proc datasets nolist nodetails; delete oned outsas three;
*********************************************************************
*  Finding Mean deviation of G is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
****                            Start New Method    *****************
****       Mean Deviation of E1
*********************************************************************;
Data three; set OnlyE1;
data three; set three; if compress(upcase(waveform))='E1' then output;
Data three; set three; proc means noprint; by insectno; var dur; output out=outsas stddev=sdE1;
data three; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert three; merge Ebert three; by insectno;
Proc datasets nolist nodetails; delete oned outsas three;
*********************************************************************
*  Finding Mean deviation of E1 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
****                            Start New Method    *****************
****       Mean Deviation of E2
*********************************************************************;
Data three; set OnlyE2;
data three; set three; if compress(upcase(waveform))='E2' then output;
Data three; set three; proc means noprint; by insectno; var dur; output out=outsas stddev=sdE2;
data three; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert three; merge Ebert three; by insectno;
Proc datasets nolist nodetails; delete oned outsas three;
*********************************************************************
*  Finding Mean deviation of E2 is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
****                            Start New Method    *****************
****       Mean Deviation of NP
*********************************************************************;
Data three; set One;
data three; set three; if compress(upcase(waveform))='NP' then output;
Data three; set three; proc means noprint; by insectno; var dur; output out=outsas stddev=sdNP;
data three; set outsas; drop _TYPE_ _FREQ_;
data Ebert; set Ebert three; merge Ebert three; by insectno;
Proc datasets nolist nodetails; delete oned outsas three;
*********************************************************************
*  Finding Mean deviation of NP is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;

*********************************************************************
****                            Start New Method    *****************
****       Mean Deviation of NP
*********************************************************************;
Data OnlyPrbsC; set OnlyPrbs;
if waveform="C" then output;
proc sort; by  insectno waveform;
proc means noprint; by  insectno; var dur; output out=Prbsout mean=MnPrbs stddev=sdPrbs median=MdnPrbs;

data three; set Prbsout; drop _TYPE_ _FREQ_;
data Ebert; set Ebert three; merge Ebert three; by insectno;
Proc datasets nolist nodetails; delete three OnlyPrbsC Prbsout;
*********************************************************************
*  Finding Mean deviation of NP is finished.
* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX     END      XXXXXXXXXXXXXXXXXXXX*
*********************************************************************;


*******************************************************************
**********    THE END. START STATISTICAL ANALYSES     *************
*******************************************************************
*******************************************************************
****             ******    ***********   *****       **************
****             ******     **********   *****   ***    ***********
****   ****************      *********   *****   ******   *********
****   ****************   *   ********   *****   ********   *******
****   ****************   **   *******   *****   *********   ******
****             ******   ***   ******   *****   **********   *****
****             ******   ****   *****   *****   *********   ******
****   ****************   *****   ****   *****   ********   *******
****   ****************   ******   ***   *****   *******   ********
****   ****************   *******   **   *****   *****   **********
****             ******   ********   *   *****   ***   ************
****             ******   *********      *****       **************
*******************************************************************
*******************************************************************

*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*****                                                      ********
*****                      Clean-up                        ********
*****                                                      ********
*****                     Treatments                       ********
*****                                                      ********
*****               Zeros and Missing data                 ********
*****                                                      ********
*****                                                      ********
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************
*******************************************************************;
Data Ebert; Set Ebert; Drop maxdur;  run;         *** Clean up file ***;
Data Ebert; Set Ebert; trt=substr(insectno,1,1); run; ***Recover treatments  ***;
Data Ebert; Set Ebert; if trt='b' then trt='a'; if trt='d' then trt='c'; run;
Data Ebert; Set Ebert;
proc export data=Ebert outfile="C:\Users\tebert\OneDrive - University of Florida\Work\Manuscipts\Gender size\Weight 2\EbertT try2FRaw.csv" dbms=csv replace;

*********************************************************************
*******                                                         *****
*******    Activate this section if treatments are fatal        *****
*******                   WARNING                               *****
*******  This simple approach only works if you do not have     *****
******* Insects in the controls that also are missing these     *****
******* behaviors.                                              *****
*********************************************************************
*********************************************************************;
/*
data Ebert; set Ebert; 
	if NumE1=0 and NumPrbsAftrFrstE=0 then NumPrbsAftrFrstE=".";
	if NumE1=0 and NmbrShrtPrbAftrFrstE=0 then NmbrShrtPrbAftrFrstE=".";
	if numE2="." then NumE2="0";
	if NumE2=0 and NumLngE1bfrE2=0 then NumLngE1bfrE2=".";
	if numlngE2="." then numlngE2="0";
	If NmbrC="." then NmbrC="0";
	If NmbrShrtC="." then NmbrShrtC="0";

	If DurG="." then DurG="0";
	if MeanG="." then MeanG="0";
	if TtlDurE="." then TtlDurE="0";
	if TtlDurE1="." then TtlDurE1="0";
	if TtlDurSnglE1="." then TtlDurSnglE1="0";
	If TtlDurE2 ="." then TtlDurE2="0";
	If MnDurE1 ="." then MnDurE1="0";
	If MnDurE2 ="." then MnDurE2="0";
	If TtlDurC="." then TtlDurC="0";
	If TtlDurNP="." then TtlDurNP="0";
	If MnDurC="." then MnDurC="0";
	If MnDurNP="." then MnDurNP="0";
	If TtlDurNP1="." then TtlDurNP1="0";
	If TtlDurNP2="." then TtlDurNP2="0";
	If TtlDurNP3="." then TtlDurNP3="0";
	If TtlDurNP4="." then TtlDurNP4="0";
	If TtlDurNP5="." then TtlDurNP5="0";
	If TtlDurNP6="." then TtlDurNP6="0";

	If CtoFrstD="." then CtoFrstD="0";
	If NumD="." then NumD="0";
	If TtlDurD="." then TtlDurD="0";
	If DurNnprbBfrFrstD="." then DurNnprbBfrFrstD="0";
	If meanD="." then meanD="0";
	If meanNDPrb="." then meanNDPrb="0";
	If TmFrmFrstPrbFrstD="." then TmFrmFrstPrbFrstD="0";
	If TmBegPrbFrstD="." then TmBegPrbFrstD="0";
	If NumPrbsAftrFrstD="." then NumPrbsAftrFrstD="0";
	If NmbrShrtPrbAftrFrstD="." then NmbrShrtPrbAftrFrstD="0";
	If NumLngD="." then NumLngD="0";
	If TmFrstSusDFrstPrb="." then TmFrstSusDFrstPrb="0";
	If maxD="." then maxD="0";
	If PrcntPrbD="." then PrcntPrbD="0";

	if meanNGPrb="." then meanNGPrb=0;
	if NumPrbsAftrFrstG="." then NumPrbsAftrFrstG=0;
	if NmbrShrtPrbAftrFrstG="." then NmbrShrtPrbAftrFrstG=0;
	if NumLngG="." then NumLngG=0;



*/;
***********************************************************************
***********************************************************************
***********************************************************************
***********************************************************************
*******                                                      **********
*******                                                      **********
*******                                                      **********
*******                   TRANSFORMATIONS                    **********
*******                                                      **********
*******                                                      **********
***********************************************************************
***********************************************************************
***********************************************************************
***********************************************************************;

run;
***********************************************************************
***********************************************************************
**** Here is a good generic set of transformations                 ****
**** Counts are sqrt transformed, durations are log transformed    ****
**** Percentages are best Logit transformed but sometimes the      ****
****     arcsine square root transformation works better.          ****
***********************************************************************
***********************************************************************;
Data Ebert; Set Ebert;
*PrcntPrbC = arsin(sqrt(PrcntPrbC/100));
*PrcntPrbE1 = arsin(sqrt(PrcntPrbE1/100));
*PrcntPrbE2 = arsin(sqrt(PrcntPrbE2/100));
*PrcntPrbG = arsin(sqrt(PrcntPrbG/100));
*PrcntE2SusE2= arsin(sqrt(PrcntE2SusE2/100));

PrcntPrbC = log((PrcntPrbC/100)/(1-PrcntPrbC/100));
PrcntPrbE1 = log((PrcntPrbE1/100)/(1-PrcntPrbE1/100));
PrcntPrbE2 = log((PrcntPrbE2/100)/(1-PrcntPrbE2/100));
PrcntPrbG = log((PrcntPrbG/100)/(1-PrcntPrbG/100));
PrcntE2SusE2 = log((PrcntE2SusE2/100)/(1-PrcntE2SusE2/100));
****************** COUNTS    *****************************;
data Ebert; set Ebert;
If transform=1 then do;
NumG=sqrt(NumG);
NumPrbsAftrFrstE=sqrt(NumPrbsAftrFrstE);
NmbrShrtPrbAftrFrstE=sqrt(NmbrShrtPrbAftrFrstE);
NumE1=sqrt(NumE1);
NumLngE1BfrE2=sqrt(NumLngE1BfrE2);
NumSnglE1=sqrt(NumSnglE1);
NumE2=sqrt(NumE2);
NumLngE2=sqrt(NumLngE2);
NumPrbs=sqrt(NumPrbs);
NmbrC=sqrt(NmbrC);
NmbrShrtC=sqrt(NmbrShrtC);
NumNP=sqrt(NumNP);
NumPrb1=sqrt(NumPrb1);
NumPrb2=sqrt(NumPrb2);
NumPrb3=sqrt(NumPrb3);
NumPrb4=sqrt(NumPrb4);
NumPrb5=sqrt(NumPrb5);
NumPrb6=sqrt(NumPrb6);
*NumPrbstofrstG=sqrt(NumPrbstofrstG);
*NumPrbstofrstD=sqrt(NumPrbstofrstD);
******** DURATIONS   ***********************************************;
Cnstnt=0.1;
TmFrstPrbFrmStrt=log(TmFrstPrbFrmStrt+Cnstnt);
CtoFrstE1=log(CtoFrstE1+Cnstnt);
DurFrstPrb=log(Durfrstprb+Cnstnt);
DurScndPrb=log(DurscndPrb+Cnstnt);
ShrtCbfrE1=log(ShrtCbfrE1+Cnstnt);
DurScndZ=log(DurScndZ+Cnstnt);
DurNnprbBfrFrstE1=log(DurNnprbBfrFrstE1+Cnstnt);
TmStrtEPGFrstE=log(TmStrtEPGFrstE+Cnstnt);
TmFrmFrstPrbFrstE=log(TmFrmFrstPrbFrstE+Cnstnt);
TmBegPrbFrstE=log(TmBegPrbFrstE+Cnstnt);
DurG=log(DurG+Cnstnt);
MeanG=log(MeanG+Cnstnt);
DurFirstE=log(DurFirstE+Cnstnt);
CntrbE1toE=log(CntrbE1toE+Cnstnt);
DurE1FlwdFrstSusE2=log(DurE1FlwdFrstSusE2+Cnstnt);
DurE1FlldFrstE2=log(DurE1FlldFrstE2+Cnstnt);
PotE2Indx=log(PotE2Indx+Cnstnt);
TtlDurE=log(TtlDurE+Cnstnt);
TtlDurE1=log(TtlDurE1+Cnstnt);
TtlDurE1FlldSusE2=log(TtlDurE1FlldSusE2+Cnstnt);
TtlDurE1FlldE2=log(TtlDurE1FlldE2+Cnstnt);
TtlDurSnglE1=log(TtlDurSnglE1+Cnstnt);
TtlDurE1FllwdE2PlsE2=log(TtlDurE1FllwdE2PlsE2+Cnstnt);
TtlDurE2=log(TtlDurE2+Cnstnt);
MnDurE1=log(MnDurE1+Cnstnt);
MnDurE2=log(MnDurE2+Cnstnt);
TtlDurC=log(TtlDurC+Cnstnt);
TotDurNnPhlPhs=log(TotDurNnPhlPhs+Cnstnt);
TtlDurNP=log(TtlDurNP+Cnstnt);
TtlPrbTm=log(TtlPrbTm+Cnstnt);
MnDurNP=log(MnDurNP+Cnstnt);
MnDurC=log(MnDurC+Cnstnt);
TmFrstSusE2=log(TmFrstSusE2+Cnstnt);
TmFrstSusE2FrstPrb=log(TmFrstSusE2FrstPrb+Cnstnt);
TmFrstSusE2StrtPrb=log(TmFrstSusE2StrtPrb+Cnstnt);
TmFrstE2StrtEPG=log(TmFrstE2StrtEPG+Cnstnt);
TmFrstE2FrmFrstPrb=log(TmFrstE2FrmFrstPrb+Cnstnt);
TmFrstE2FrmPrbStrt=log(TmFrstE2FrmPrbStrt+Cnstnt);
TtlDurNp1=log(TtlDurNp1+Cnstnt);
TtlDurNp2=log(TtlDurNp2+Cnstnt);
TtlDurNp3=log(TtlDurNp3+Cnstnt);
TtlDurNp4=log(TtlDurNp4+Cnstnt);
TtlDurNp5=log(TtlDurNp5+Cnstnt);
TtlDurNp6=log(TtlDurNp6+Cnstnt);
sdNP=log(sdNP+cnstnt);
sdC=log(sdC+cnstnt);
sdD=log(sdD+cnstnt);
sdG=log(sdG+cnstnt);
sdE1=log(sdE1+cnstnt);
sdE2=log(sdE2+cnstnt);
**************     The following are the non-Sarria variables        *******;
CtoFrstD=log(CtoFrstD+cnstnt);
NumD=sqrt(NumD);
TtlDurD=log(TtlDurD+cnstnt);
DurNnprbBfrFrstD=log(DurNnprbBfrFrstD+cnstnt);
meanD=Log(meanD+cnstnt);
meanNDPrb=sqrt(meanNDPrb);
TmFrmFrstPrbFrstD=Log(TmFrmFrstPrbFrstD+cnstnt);
TmBegPrbFrstD=Log(TmBegPrbFrstD+cnstnt);
NumPrbsAftrFrstD=Sqrt(NumPrbsAftrFrstD);
NmbrShrtPrbAftrFrstD=Sqrt(NmbrShrtPrbAftrFrstD);
NumLngD=sqrt(NumLngD);
TmFrstSusDFrstPrb=Log(TmFrstSusDFrstPrb+cnstnt);
maxD=Log(maxD+cnstnt);
CtoFrstG= log(CtoFrstG+cnstnt);
DurNnprbBfrFrstG=log(DurNnprbBfrFrstG+cnstnt);
meanNGPrb=sqrt(meanNGPrb);
TmFrmFrstPrbFrstG=log(TmFrmFrstPrbFrstG+cnstnt);
TmBegPrbFrstG=log(TmBegPrbFrstG+cnstnt);
NumPrbsAftrFrstG=sqrt(NumPrbsAftrFrstG);
NmbrShrtPrbAftrFrstG=sqrt(NmbrShrtPrbAftrFrstG);
NumLngG=log(NumLngG+cnstnt);
TmFrstSusGFrstPrb=log(TmFrstSusGFrstPrb+cnstnt);
MnPrbs=log(MnPrbs+Cnstnt);
sdPrbs=log(sdPrbs+Cnstnt);
MdnPrbs=log(MdnPrbs+Cnstnt);
if PrcntPrbD="." then PrcntPrbD=0;
PrcntPrbD=Arsin(sqrt(PrcntPrbD/100));
TmLstE2EndRcrd=log(TmLstE2EndRcrd+cnstnt);
maxE2=log(MaxE2+cnstnt);
DurNpFllwFrstSusE2=log(DurNpFllwFrstSusE2+cnstnt);
SumD=log(SumD+cnstnt);
end;


Data Ebert; Set Ebert; drop Cnstnt;
*/;

*******************************************************************
**********    THE END. START STATISTICAL ANALYSES     *************
*******************************************************************
*******************************************************************
****             ******    ***********   *****       **************
****             ******     **********   *****   ***    ***********
****   ****************      *********   *****   ******   *********
****   ****************   *   ********   *****   ********   *******
****   ****************   **   *******   *****   *********   ******
****             ******   ***   ******   *****   **********   *****
****             ******   ****   *****   *****   *********   ******
****   ****************   *****   ****   *****   ********   *******
****   ****************   ******   ***   *****   *******   ********
****   ****************   *******   **   *****   *****   **********
****             ******   ********   *   *****   ***   ************
****             ******   *********      *****       **************
*******************************************************************
*******************************************************************

**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
************                                                      ****************
************                                                      ****************
************                                                      ****************
************                    DATA ANALYSES                     ****************
************                                                      ****************
************                                                      ****************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************;


/***********************************************************************************************
************************************************************************************************
**                                                                                            **
** The Following uses the default LSD Test. If you want Tukey, highlight, and global replace  **
** "lines;" with "lines adjust=Tukey;" and then "replace all in selection."                   **
**                                                                                            **
************************************************************************************************/;
************************************************************************************************
**       NOTE THIS PROGRAM IS REORGANIZED LIST BASED ON EBERT 2.0 08212015.SAS.               **
**              It is based on the SAS mimic of the Sarria workbook, but                      **
**              the order of the variables has been changed. Furthermore,                     **
**              the variables not relevant to psyllids have been removed.                     **
**              The SAS sataset Ebert still retains the names of removed                      **
**              variables, so that it would be possible to return them to the program         **
**              with another Proc glimmix statement (should they become needed).              **
**                                                                                            **
************************************************************************************************;

*******************************************************************;
********      Probe Level Variables       *************************;
*******************************************************************;
*This will save the data file for transformed values;
proc export data=Ebert outfile="C:\Users\tebert\OneDrive - University of Florida\Work\Manuscipts\Gender size\Weight 2\EbertT try2FTran.csv" dbms=csv replace;
